我是 android 的初学者,正在尝试自己制作一个简单的应用程序。我试图发布尽可能多的代码细节。
当我单击中的显示任务按钮时MainActivity.java
,它不会启动新的 Intent 并抛出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();
}
}
任务列表.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;
}
}
}