我的首选项屏幕无法在真实设备上运行

时间:2016-05-20 11:24:52

标签: java android preferenceactivity

在我的应用程序中,用户可以通过按下按钮打开设置菜单(这是一个PreferenceActivity)。这在AVD上运行良好,但是当我尝试在真实设备上使用它时它不起作用。我不知道这是怎么发生的,我希望有人可以帮助我。

这是我的PreferenceActivity:

public class ActivityPref extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref);
        Preference preferences=findPreference("key");
        preferences.setIntent(new Intent(this, PwdChange.class));
        showNoti();
        checkForBackUp();
    }

    private void checkForBackUp() {
        final CheckBoxPreference cb = (CheckBoxPreference) getPreferenceManager().findPreference("backUp");
        cb.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                if(preference.isEnabled()){
                    final String inFileName = "/data/data/com.sus.pwd/databases/my_d";
                    String outFileName = Environment.getExternalStorageDirectory()+"/database_copy.db";
                    OutputStream output = null;
                    FileInputStream fis=null;
                    File dbFile = new File(inFileName);
                    try {
                        fis = new FileInputStream(dbFile);
                        output = new FileOutputStream(outFileName);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }

                    // Transfer bytes from the inputfile to the outputfile
                    byte[] buffer = new byte[1024];
                    int length;
                    try {
                        while ((length = fis.read(buffer))>0){
                            try {
                                output.write(buffer, 0, length);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    try {
                        output.flush();
                        output.close();
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return true;
            }
        });
    }

    private void showNoti() {
        final CheckBoxPreference cb = (CheckBoxPreference) getPreferenceManager().findPreference("key1");

        cb.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                boolean myValue = (Boolean) newValue;

                if (newValue.toString().equals("true")) {
                    Context context = getApplicationContext();
                    Toast.makeText(context, "start..", Toast.LENGTH_SHORT).show();
                    Log.e("value", String.valueOf(myValue));
                    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                            .setSmallIcon(R.drawable.delete);

                    Intent intent = new Intent(context, MainActivity.class);
                    PendingIntent pIntent = PendingIntent.getActivity(context, 1, intent, 0);
                    builder.setContentIntent(pIntent);
                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    Notification notif = builder.build();
                    mNotificationManager.notify(1, notif);
                } else if (newValue.toString().equals("false")) {
                    Toast.makeText(getApplicationContext(), "stop..", Toast.LENGTH_SHORT).show();
                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    mNotificationManager.cancel(1);
                }
                return true;
            }
        });
    }
}

这是我的主要活动:

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id==R.id.settings){
        startActivity(new Intent(this, ActivityPref.class));          
    }
}

这是LogCat:

05-20 17:28:19.476 11771-11771/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sushanta.reminderpwd, PID: 11771
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sushanta.reminderpwd/com.sushanta.reminderpwd.ActivityPref}: android.view.InflateException: Binary XML file line #41: Error inflating class java.lang.reflect.Constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class java.lang.reflect.Constructor
at android.preference.GenericInflater.createItem(GenericInflater.java:397)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:276)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1512)
at com.sushanta.reminderpwd.ActivityPref.onCreate(ActivityPref.java:29)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5292) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417) 
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428) 
at android.preference.GenericInflater.rInflate(GenericInflater.java:481) 
at android.preference.GenericInflater.rInflate(GenericInflater.java:493) 
at android.preference.GenericInflater.inflate(GenericInflater.java:326) 
at android.preference.GenericInflater.inflate(GenericInflater.java:263) 
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:276) 
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1512) 
at com.sushanta.reminderpwd.ActivityPref.onCreate(ActivityPref.java:29) 
at android.app.Activity.performCreate(Activity.java:5264) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5292) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:228)
at android.content.res.Resources.getTextArray(Resources.java:458)
at android.content.res.TypedArray.getTextArray(TypedArray.java:629)
at android.preference.ListPreference.<init>(ListPreference.java:51)
at java.lang.reflect.Constructor.constructNative(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
at android.preference.GenericInflater.createItem(GenericInflater.java:383) 
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417) 
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428) 
at android.preference.GenericInflater.rInflate(GenericInflater.java:481) 
at android.preference.GenericInflater.rInflate(GenericInflater.java:493) 
at android.preference.GenericInflater.inflate(GenericInflater.java:326) 
at android.preference.GenericInflater.inflate(GenericInflater.java:263) 
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:276) 
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1512) 
at com.sushanta.reminderpwd.ActivityPref.onCreate(ActivityPref.java:29) 
at android.app.Activity.performCreate(Activity.java:5264) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5292) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
at dalvik.system.NativeStart.main(Native Method) 
05-20 17:28:21.049 25577-25577/? E/NotificationService: unable to notify listener (removed): android.service.notification.INotificationListener$Stub$Proxy@428150b0
android.os.DeadObjectException
at android.os.BinderProxy.transact(Native Method)
at android.service.notification.INotificationListener$Stub$Proxy.onNotificationRemoved(INotificationListener.java:120)
at com.android.server.NotificationManagerService$NotificationListenerInfo.notifyRemovedIfUserMatch(NotificationManagerService.java:273)
at com.android.server.NotificationManagerService$3.run(NotificationManagerService.java:854)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at com.android.server.ServerThread.initAndLoop(SystemServer.java:1452)
at com.android.server.SystemServer.main(SystemServer.java:1547)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)

// pref.xml

<Preference
    android:title="Change Master password"
    android:key="key"/>

<CheckBoxPreference
    android:title="Notification on Action Bar"
    android:key="key1"
    android:defaultValue="true"/>
<CheckBoxPreference
    android:title="Back up"
    android:key="backUp"
    android:summary="Load data to external device"
    android:defaultValue="false"/>
<CheckBoxPreference
    android:title="Auto Exit"
    android:summary="The Application will automatically exit when the screen turns off"
    android:defaultValue="true"/>

0 个答案:

没有答案