Question

I am trying to set the onclick listener that will trigger a dialog, but some error has appeared in my code. It is that I assign wrongly the listener. since,it appear with the null pointerexception error,how'd it happen?

here is my logcat error list.

05-15 18:42:38.273: E/AndroidRuntime(27345): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.checklist/com.example.checklist.MainActivity}: java.lang.NullPointerException

05-15 18:55:35.361: E/AndroidRuntime(28319): Caused by: java.lang.NullPointerException
05-15 18:55:35.361: E/AndroidRuntime(28319):    at com.example.checklist.MainActivity.onCreate(MainActivity.java:55)

here is my code

public class MainActivity extends Activity implements OnClickListener{

ArrayList<Product> products = new ArrayList<Product>();

ListAdapter boxAdapter;


  /** Called when the activity is first created. */
  public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    fillData();
    boxAdapter = new ListAdapter(this, products);

    ListView lvMain = (ListView) findViewById(R.id.lvMain);
    lvMain.setAdapter(boxAdapter);


    Button btn = (Button) findViewById(R.id.insert);

    OnClickListener listener = new OnClickListener() {          
        @Override
        public void onClick(View v) {                               

            Dialog d = onCreateDialog(savedInstanceState);
            d.show();
        }
    };

    /** Setting the event listener for the add button */
    btn.setOnClickListener(listener);       



  }

  void fillData() {

      String[] dataArray = getResources().getStringArray(R.array.ChecklistData);
      for(String productName : dataArray)
      {
        products.add(new Product(productName,false));
      }

  }







  public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      // Get the layout inflater
      LayoutInflater inflater = this.getLayoutInflater();

      // Inflate and set the layout for the dialog
      // Pass null as the parent view because its going in the dialog layout
      builder.setView(inflater.inflate(R.layout.dialog, null))
      // Add action buttons
             .setPositiveButton(R.string.Insert, new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int id) {
                   //get the text from the setText and insert it into the arrayList name products


                 }
             })
             .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                    // DialogFragment.this.getDialog().cancel();
                 }
             });      
      return builder.create();
  }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}






  /*public void showResult(View v) {
    String result = "Selected Product are :";
    int totalAmount=0;
    for (Product p : boxAdapter.getBox()) {
      if (p.box){
        result += "\n" + p.name;
        //totalAmount+=p.price;
      }
    }
  //  Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
  }*/
}

here is my dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >


<ImageView
    android:src="@drawable/ic_launcher"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:scaleType="center"
    android:background="#FFFFBB33"
    android:contentDescription="@string/app_name" />

  <EditText
    android:id="@+id/insert"
    android:inputType="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="@string/ItemName" />
    </LinearLayout>
Was it helpful?

Solution 2

You do not have a button with id insert in the listview.xml you posted. So btn is null and you would be calling setOnClickListener(listener) on a null object.

Add

<Button
android:id="@+id/insert"
android:layout_width="wrap_content"

OTHER TIPS

Your listview.xml layout does not have a view with id insert.

You probably want to add

android:id="@+id/insert"

to the Button there.

Also remove the onClick attribute from XML since you've commented the code out.


Edit based on comment:

insert was with my dialog.xml

Then call findViewById() etc. on the layout you inflate while creating your dialog, e.g.

View layout = inflater.inflate(R.layout.dialog, null);
Button btn = layout.findViewById(...);
btn.setOnClickListener(...);
builder.setView(layout)

Edit 2 based on dialog.xml:

insert is an EditText, not a Button. Please make up your mind :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top