带有AlertDialog的NullpointerException / InvocationTargetException

时间:2014-05-24 12:02:14

标签: android alertdialog invocationtargetexception

单击AlertDialog上的“Positive”按钮时出现此异常。已经看过" Android Dialog InvocationTargetException"问题,但据我所知,建议的答案并不适用。 以下代码属于非主要活动。该应用程序是一个简单的SQLite应用程序,基于此处的教程: http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/

控制台的输出在此处,然后是相关活动的代码。我对Java和Android应用程序开发完全陌生,所以如果我发现了一个非常基本的错误,我会道歉!

05-24 07:53:45.472: E/AndroidRuntime(1547): FATAL EXCEPTION: main
05-24 07:53:45.472: E/AndroidRuntime(1547): Process: com.ian.mealtimer, PID: 1547
05-24 07:53:45.472: E/AndroidRuntime(1547): java.lang.NullPointerException
05-24 07:53:45.472: E/AndroidRuntime(1547):     at com.ian.mealtimer.PresetItemsActivity$1$1.onClick(PresetItemsActivity.java:62)
05-24 07:53:45.472: E/AndroidRuntime(1547):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
05-24 07:53:45.472: E/AndroidRuntime(1547):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-24 07:53:45.472: E/AndroidRuntime(1547):     at android.os.Looper.loop(Looper.java:136)
05-24 07:53:45.472: E/AndroidRuntime(1547):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-24 07:53:45.472: E/AndroidRuntime(1547):     at java.lang.reflect.Method.invokeNative(Native Method)
05-24 07:53:45.472: E/AndroidRuntime(1547):     at java.lang.reflect.Method.invoke(Method.java:515)
05-24 07:53:45.472: E/AndroidRuntime(1547):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-24 07:53:45.472: E/AndroidRuntime(1547):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-24 07:53:45.472: E/AndroidRuntime(1547):     at dalvik.system.NativeStart.main(Native Method)

活动代码:

public class PresetItemsActivity extends Activity {
    private ListView obj;
    MySQLiteHelper mydb;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preset_items_activity_main);

        //Retrieve Preset_Items from database and bind to ListView
        mydb = new MySQLiteHelper(this);
        ArrayList array_list = mydb.getAllPresetItems();
        ArrayAdapter arrayAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, array_list);
        // Add it to the list view.
        ListView lv = (ListView) findViewById(R.id.list);
        lv.setAdapter(arrayAdapter);

        //Handle click on ADD button
        Button AddButton = (Button) findViewById(R.id.add); 
        // Setup event handlers
        AddButton.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) {
                final Context context = view.getContext(); 

                //Inflate the preset_items form
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View formElementsView = inflater.inflate(R.layout.preset_item_detail, null, false);
                //Get data entry form fields
                final EditText txtPresetName = (EditText) formElementsView.findViewById(R.id.txtPresetName);
                final EditText txtPresetMinutes = (EditText) formElementsView.findViewById(R.id.txtPresetMinutes);

                //Create and AlertDialog with the inflated preset_item_detail layout
                new AlertDialog.Builder(context)
                .setView(formElementsView)
                .setTitle("Create Meal Ingredient")
                .setPositiveButton("Add",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            String PresetName = txtPresetName.getText().toString();
                            int iMinutes = Integer.parseInt(txtPresetMinutes.getText().toString());
                            preset_item preset_item = new preset_item();
                            preset_item.preset_desc = PresetName;
                            preset_item.preset_minutes = iMinutes;
                            //Save new preset_item record
                            boolean createSuccessful = new TableControllerPresetItem(context).create(preset_item);
                            if(createSuccessful){
                                Toast.makeText(context, "Information was saved.", Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(context, "Unable to save information.", Toast.LENGTH_SHORT).show();
                            }
//                          dialog.cancel();
                        }

                    }).show();
        } 
        });

        //Handle click on DELETE button
        Button DeleteButton = (Button) findViewById(R.id.delete); 
        // Setup event handlers
        DeleteButton.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) { 
                EditText txtDeleteId = (EditText) findViewById(R.id.txtPresetMinutes);
                int DeleteId = Integer.parseInt(txtDeleteId.getText().toString());
                mydb.deletePresetItem(DeleteId);
                txtDeleteId.getEditableText().clear();
                Toast.makeText(getApplicationContext(), "Deleted",
                        Toast.LENGTH_SHORT).show();
        } 
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

}

这是我的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/txtPresetName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:hint="Item name"
        android:singleLine="true" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/txtMinutes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:inputType="number"
        android:layout_below="@+id/txtPresetName"
        android:hint="Cooking time (minutes)"
        android:singleLine="true" />

</RelativeLayout>

0 个答案:

没有答案