启动意图时出现空指针异常

时间:2013-10-16 08:46:48

标签: java android nullpointerexception

我是android的初学者,并尝试自己创建一个简单的应用程序。我试图发布代码的详细信息。

当我单击MainActivity.java中的显示任务按钮时,它将不会启动新的意图并抛出NullPointerException。如果我在setOnClickListener中删除TaskList.java及与其相关的代码,则新的Intent将启动,但按钮将无效。我已经发布了日志以便于使用。

MainActivity.java

package com.budthapa.unsavedchanges;

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private Button addTask;
    private Button cancel;
    private EditText editText;
    private Button showTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();

        addTask.setOnClickListener(this);
        showTask.setOnClickListener(this);
        cancel.setOnClickListener(this);

    }

    private void initialize() {
        addTask = (Button) findViewById(R.id.btnAddTask);
        showTask = (Button) findViewById(R.id.btnShowTask);
        cancel = (Button) findViewById(R.id.btnCancel);
        editText = (EditText) findViewById(R.id.editText);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        String text = editText.getText().toString();

        // cancel button works only if textbox is not empty
        switch (arg0.getId()) {
        case R.id.btnAddTask:
            // call method that add tasks
            if (text.isEmpty()) {
                // if no content in the textbox
                Toast showMessage = Toast.makeText(MainActivity.this,
                        "Please fill some task to add", Toast.LENGTH_LONG);
                showMessage.setGravity(Gravity.CENTER, 0, 0);
                showMessage.show();
            } else {
                addTask();
            }
            break;

        case R.id.btnShowTask:
            // It will start a new activity to show the list of task
            Intent showTaskList = new Intent(MainActivity.this, TaskList.class);
            startActivity(showTaskList);
            break;
        case R.id.btnCancel:
            // call this method when user creates cancel button
            if (!text.isEmpty()) {
                // there is some content in the text box
                unSavedDialog();
                break;

            }
            // you can call this code instead, but will not so any warning
            // finish();
            exitAppDialog(); // this code will show alert dialog box
        }
    }

    private void addTask() {
        // add new item to the array list
        String taskName = editText.getText().toString();
        ArrayList<String> taskArr = new ArrayList<String>();
        taskArr.add(taskName);
        // notify user that new task is added
        Toast addNewTask = Toast.makeText(MainActivity.this,
                "New Task Added successfully", Toast.LENGTH_SHORT);
        addNewTask.setGravity(Gravity.CENTER, 0, 0);
        addNewTask.show();
    }

    private void exitAppDialog() {
        // this method will start when user clicks cancel button
        AlertDialog.Builder exitDialog = new AlertDialog.Builder(this);
        exitDialog.setTitle(R.string.exitApp);
        exitDialog.setMessage(R.string.exitMessage);
        exitDialog.setCancelable(false);

        exitDialog.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // exit the app
                        finish();
                    }
                });
        exitDialog.setNegativeButton("No",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int arg1) {
                        // cancel and return to the app
                        dialog.cancel();
                    }
                });
        AlertDialog alert = exitDialog.create();
        alert.show();
    }

    private void unSavedDialog() {
        // this dialog will appear when user press cancel button
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle(R.string.alertTitle);
        alert.setMessage(R.string.alertMessage);
        alert.setCancelable(false);

        alert.setNegativeButton("Add Task",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // save the task and exit
                        addTask();
                    }
                });

        alert.setPositiveButton("Discard Changes",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // finish the activity
                        finish();
                    }
                });

        alert.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // cancel the dialog box and return to the view
                arg0.cancel();
            }
        });

        AlertDialog dialog = alert.create();
        dialog.show();
    }
}

TaskList.java

package com.budthapa.unsavedchanges;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TaskList extends Activity implements OnClickListener {
    private Button addNewTask;
    private Button cancel;
    private TextView taskList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_list);
        initialize();

        addNewTask.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    private void initialize() {
        // TODO Auto-generated method stub
        addNewTask = (Button) findViewById(R.id.btnAddTask);
        cancel = (Button) findViewById(R.id.btnCancel);
        taskList = (TextView) findViewById(R.id.textView);
    }

    public void showTask() {

    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.btnCancel:
            AlertDialog.Builder alert = new AlertDialog.Builder(TaskList.this);
            alert.setTitle("Do you want to exit?");
            alert.setCancelable(false);
            alert.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            finish();
                        }
                    });

            alert.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            arg0.cancel();
                        }
                    });

            AlertDialog dialog = alert.create();
            dialog.show();
            break;

        case R.id.btnAddTask:
            Intent showMain = new Intent(TaskList.this, MainActivity.class);
            startActivity(showMain);

            finish(); // finish the current running activity and go back to main
                        // Activity
            break;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个..

有两个相同的btns

addNewTask = (Button) findViewById(R.id.btnAddTask);
cancel = (Button) findViewById(R.id.btnCancel);

TaskList.class

task_list.xml没有btnAddTask该错误的原因

您需要为不同的活动提供不同名称的xml btn id

答案 1 :(得分:0)

// MainActivity.java

case R.id.btnCancel:
            // call this method when user creates cancel button
            if (!text.isEmpty()) {

                unSavedDialog();
                break;

            }

            exitAppDialog(); // this code will show alert dialog box
         break;   <------- MISSING......
        }
    }