2013-10-16 4 views
-2

Je suis un débutant dans Android et essaie de faire une application simple de mon propre. J'ai essayé d'afficher autant de détails du code.Null Pointer Exception au début de l'intention

Lorsque je clique sur le bouton Afficher la tâche dans MainActivity.java, il ne démarre pas la nouvelle intention et lance le NullPointerException. Si j'enlève le setOnClickListener et le code qui s'y rapporte dans TaskList.java, la nouvelle intention commencera mais les boutons ne fonctionneront pas. J'ai posté les journaux pour le rendre facile.

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; 
     } 
    } 
} 
+3

Veuillez publier le logcat. – Kristopher

+0

@Selvin E/AndroidRuntime (11060): EXCEPTION FATALE: principale E/AndroidRuntime (11060): java.lang.RuntimeException: Impossible de démarrer l'activité ComponentInfo {com.budthapa.unsavedchanges/com.budthapa.unsavedchanges.TaskList}: * * java.lang.NullPointerException ** E/AndroidRuntime (11060): à l'adresse android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2211) E/AndroidRuntime (11060): at android.app.ActivityThread.handleLaunchActivity (ActivityThread. java: 2261) E/AndroidRuntime (11060): at android.app.ActivityThread.access $ 600 (ActivityThread.java:141) – budthapa

Répondre

0

Try this ..

il y a deux mêmes btns

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

TaskList.class

task_list.xml ne pas btnAddTask que la raison pour laquelle cette erreur

Vous devez donner id xml BTN pour nom différent pour une activité différente

+0

Mais, si ces ID n'existent pas, cela ne devrait pas provoquer une erreur de compilation? –

+0

@angel_navarro btnAddTask existe déjà dans activity_main.xml alors btnAddTask ne doit pas donner ce nom à d'autres actvity.xml s'il le fait, il fera NPE. – Hariharan

+0

Ahh ok @Tamilan, que pour votre clarification ;-) –

0

de //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...... 
     } 
    } 
+1

Ce "cas" est le dernier sur ce "commutateur()", de sorte que cela n'aura pas d'effet –