2017-01-09 17 views
-1

"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) 
+0

처럼 글고 초기화한다! – Mrinmoy

+0

ur EditText 선언에서 가져 오는 null 포인터 예외입니다. 귀하의 코드를 디버그하고 쉽게 UR 오류있어. – Andrain

답변

0

당신은 글고 제대로 초기화되지

View dialogView = inflater.inflate(R.layout.pojedyncze_zadanie,null); 
    builder.setView(dialogView); 

    EditText input1 = (EditText) dialogView .findViewById(R.id.tematZadania); 
+0

아주 간단했습니다. 나는 지금 당황 해요;) 시간 내 주셔서 감사합니다! – Graphite

1

input1.getText()

에 응용 프로그램이 충돌합니다. 그래서 귀하의 오류 입력 1에 따르면, 당신이 지나가는 "id"가 맞는지 확인하십시오 findViewById()입력 2에 대해서도 적절합니다. 이 같은

과 정의에 AlertDialog는, 당신이해야 할 일 : -

EditText input1 = (EditText) dialog.findViewById(.....);