任何人有一個想法,爲什麼應用程序崩潰後點擊「Dodaj」? 當我使用默認值taskArray例如。 taskArray(新任務( 「例如」, 「例如」)一切順利。在下面的情況我警告對話框崩潰的應用程序。自定義AlerDialog
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
final int id = item.getItemId();
final EditText input1 = (EditText) findViewById(R.id.tematZadania);
final EditText input2 = (EditText) findViewById(R.id.prioZadania);
if (id == R.id.menuItem) {
AlertDialog noweZadanie = new AlertDialog.Builder(MainActivity.this)
.setTitle("Dodaj nowe zadanie")
.setView(R.layout.pojedyncze_zadanie)
.setPositiveButton("Dodaj", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "ej", Toast.LENGTH_SHORT).show();
String item1 = String.valueOf(input1.getText());
String item2 = String.valueOf(input2.getText());
taskArray.add(new Task(item1, item2));
//input1.setText("");
//input2.setText("");
taskArrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("Anuluj", null)
.create();
noweZadanie.show();
}
if (id == R.id.menuItem2) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
XML.file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/tematZadania" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/prioZadania" />
</LinearLayout>
登錄
I/Timeline: Timeline: Activity_idle id: [email protected] time:204086971
W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
I/ListPopupWindow: Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.piotr.todolist, PID: 2616
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.piotr.todolist.MainActivity$2.onClick(MainActivity.java:176)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5319)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
你的EditText沒有正確的初始化! – Mrinmoy
它從你的EditText聲明中獲得一個空指針異常。調試你的代碼,你很容易出錯。 – Andrain