1

我有一个保存按钮,它加载一个对话框界面,要求保存名称。当您单击完成时,它应该创建一个包含一些字符串的共享首选项,但它只是说不幸的是程序已停止。我怎样才能解决这个问题?哦,我只是想把它放在那里,我是 android 新手。我在制作应用程序时正在学习所有这些东西。

public void save(View view){

    LayoutInflater li = LayoutInflater.from(Calculate.this);
    View pView = li.inflate(R.layout.prompt, null);

    AlertDialog.Builder adb = new AlertDialog.Builder(Calculate.this);
    adb.setView(pView);

    final EditText name = (EditText) findViewById(R.id.etPrompt);

    adb.setCancelable(false);
    adb.setPositiveButton("Done", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            n = name.getText().toString();
            try {
                details = getSharedPreferences(n,MODE_PRIVATE);
                editor = details.edit();
                editor.putString("ebp", ebp.getText().toString());
                editor.putString("fb", fb.getText().toString());
                editor.putString("sv", sv.getText().toString());
                editor.putString("pv", pv.getText().toString());
                editor.putString("sl", sl.getText().toString());
                editor.putString("cl", cl.getText().toString());
                editor.commit();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();

        }
    });

    AlertDialog ad = adb.create();
    ad.show();


}

日志猫

05-18 20:54:28.300: E/AndroidRuntime(16575): FATAL EXCEPTION: main

05-18 20:54:28.300: E/AndroidRuntime(16575): java.lang.NullPointerException

05-18 20:54:28.300: E/AndroidRuntime(16575):    at                   com.TBJsoft.enclosurecalculator.Calculate$1.onClick(Calculate.java:103)

05-18 20:54:28.300: E/AndroidRuntime(16575):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)

05-18 20:54:28.300: E/AndroidRuntime(16575):    at android.os.Handler.dispatchMessage(Handler.java:99)

05-18 20:54:28.300: E/AndroidRuntime(16575):    at android.os.Looper.loop(Looper.java:137)

05-18 20:54:28.300: E/AndroidRuntime(16575):    at android.app.ActivityThread.main(ActivityThread.java:5041)

05-18 20:54:28.300: E/AndroidRuntime(16575):    at java.lang.reflect.Method.invokeNative(Native Method)

05-18 20:54:28.300: E/AndroidRuntime(16575):    at java.lang.reflect.Method.invoke(Method.java:511)

05-18 20:54:28.300: E/AndroidRuntime(16575):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

05-18 20:54:28.300: E/AndroidRuntime(16575):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

05-18 20:54:28.300: E/AndroidRuntime(16575):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

您需要使用 yourActivities context来访问SharedPreferences您的AlertDialog. 因此,您可以在外部初始化它们AlertDialog或将其更改为

   details =  Caluclate.this.getSharedPreferences(n,MODE_PRIVATE);  // assuming that Calculate is the name of your `Activity`

如果这没有帮助,请发布 logcat 以便我们知道它在哪里崩溃

编辑

改变

final EditText name = (EditText) findViewById(R.id.etPrompt);

final EditText name = (EditText) pView.findViewById(R.id.etPrompt);
于 2013-05-18T20:58:58.403 回答