坚持创造一个偏好

时间:2014-09-26 14:15:05

标签: android

我正在创建包含3个选项的首选项

  1. 关于我们

  2. 与我们联系

  3. 帮助

  4. 当我在模拟器上运行以下代码,然后打开应用程序并点击关于我们时,我收到一条消息说"不幸的是Rost已停止工作"。

    关于我们应该显示一个带有文字的对话框主题"这是一个试用版完整版将花费你大约1000美元"

    想知道可能是什么问题。

    这是我的代码:

    Prefs.java

    import android.os.Bundle;
    import android.preference.PreferenceFragment;
    
    
    class Prefs extends PreferenceFragment {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
    
    
            addPreferencesFromResource(R.xml.simplr_preferences);
    
    
        }
    

    About.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This ia a demo version full version will cost you around $1000" />
    
    </LinearLayout>
    

    Rost manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.world.rost"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="21" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".RostApp"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity
                android:name=".Menu"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="com.world.rost.MENU" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
            <activity
                android:theme="@android:style/Theme.Dialog"
                android:name=".About"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="com.world.rost.ABOUT" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
            <activity
                android:theme="@android:style/Theme.Dialog"
                android:name=".Prefs"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="com.world.rost.PREFS" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
    
    
    
        </application>
    
    </manifest>
    

    simplr_preferences.xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <PreferenceCategory
            android:title="Foo" >  
    
    
        <EditTextPreference
            android:key="edit"
            android:summary="This ia edit text"
            android:title="edittext" />
    
        <CheckBoxPreference
             android:title="Checkbox" >
             android:key="Checkbox"
             android:defaultvalue="true"
            android:summary="This ia a check box"
    
        </CheckBoxPreference>
    
        <ListPreference
            android:title="Listpreference"
            android:key="list"
            android:summary="this is a list"
            android:entries="@array/list" />    
    
    </PreferenceCategory>
    
    </PreferenceScreen>
    

    menu.java

    package com.world.rost;
    import android.app.ListActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    
    
    
    public class Menu extends ListActivity{
    
        String classes[] = {"MainActivity","Dummyone","Camera","Email","Data","Openedclass" };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
    
            setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classes));
        }
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            // TODO Auto-generated method stub
            super.onListItemClick(l, v, position, id);
            String name = classes[position];
            try {
    
                Class myclass = Class.forName("com.world.rost." + name);
    
                Intent newintent = new Intent(Menu.this,myclass);
                startActivity(newintent);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(android.view.Menu menu) {
            // TODO Auto-generated method stub
            super.onCreateOptionsMenu(menu);
            MenuInflater disp = getMenuInflater();
            disp.inflate(R.menu.simple_menu, menu);
    
            return true;
    
    
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub
    
            switch(item.getItemId())
            {
    
            case R.id.aboutUs:
             Intent isn = new Intent("com.world.rost.PREFS");
             startActivity(isn);
    
            break;
    
            case R.id.contactUs:
    
            break;
    
    
            case R.id.help:
            finish();
    
            break;
    
            }
            return false;
    
        }
    }
    

    logcat错误日志

    09-26 09:50:20.970: D/dalvikvm(1605): GC_FOR_ALLOC freed 16K, 4% free 3364K/3476K, paused 3ms, total 4ms
    09-26 09:50:20.970: I/dalvikvm-heap(1605): Grow heap (frag case) to 3.979MB for 635808-byte allocation
    09-26 09:50:21.000: D/dalvikvm(1605): GC_FOR_ALLOC freed 1K, 3% free 3984K/4100K, paused 25ms, total 25ms
    09-26 09:50:21.090: W/EGL_emulation(1605): eglSurfaceAttrib not implemented
    09-26 09:52:26.362: W/EGL_emulation(1605): eglSurfaceAttrib not implemented
    09-26 09:52:27.102: D/dalvikvm(1605): newInstance failed: Lcom/world/rost/Prefs; not accessible to Landroid/app/Instrumentation;
    09-26 09:52:27.102: D/AndroidRuntime(1605): Shutting down VM
    09-26 09:52:27.102: W/dalvikvm(1605): threadid=1: thread exiting with uncaught exception (group=0xb2d2db20)
    09-26 09:52:27.102: E/AndroidRuntime(1605): FATAL EXCEPTION: main
    09-26 09:52:27.102: E/AndroidRuntime(1605): Process: com.world.rost, PID: 1605
    09-26 09:52:27.102: E/AndroidRuntime(1605): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.world.rost/com.world.rost.Prefs}: java.lang.IllegalAccessException: access to class not allowed
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.os.Handler.dispatchMessage(Handler.java:102)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.os.Looper.loop(Looper.java:136)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at java.lang.reflect.Method.invokeNative(Native Method)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at java.lang.reflect.Method.invoke(Method.java:515)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at dalvik.system.NativeStart.main(Native Method)
    09-26 09:52:27.102: E/AndroidRuntime(1605): Caused by: java.lang.IllegalAccessException: access to class not allowed
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at java.lang.Class.newInstanceImpl(Native Method)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at java.lang.Class.newInstance(Class.java:1208)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
    09-26 09:52:27.102: E/AndroidRuntime(1605):     ... 11 more
    

2 个答案:

答案 0 :(得分:0)

再次检查首选项XML文件!有一些XML语法错误!

<CheckBoxPreference
         android:title="Checkbox" 
         android:key="Checkbox"
         android:defaultvalue="true"
        android:summary="This ia a check box" >

</CheckBoxPreference>

之后,当您指向的文件不是Activity时,您无法启动Activity

您的偏好设置文件是PreferenceFragment,需要托管在FragmentActivityActivityFragment

请务必再次查看官方文档http://developer.android.com/guide/topics/ui/settings.html

答案 1 :(得分:0)

这是你的主要错误。您无法以意图启动Fragment

Intent isn = new Intent("com.world.rost.PREFS");
  

Intent可用于启动Activity,broadcastIntent可将其发送给任何感兴趣的BroadcastReceiver组件,startService(Intent)或bindService(Intent,ServiceConnection,int)可与后台服务进行通信。

您需要在Fragment或相关课程中托管FragmentActivity

"PREFS" is not an Activity inside the package "com.world.rost"

这是Fragment

  

片段必须始终嵌入到活动中,片段的生命周期直接受主机活动生命周期的影响。

这是related question that can help you

另外,正如 @Pavlos 所提到的,你也有一些语法XML错误。