App crashes due to nullpointerexception

Put your problem here if it does not fit any of the other categories.

App crashes due to nullpointerexception

Postby cg2916 » Mon Jan 17, 2011 6:50 pm

I want to make it so that a user taps a button, it opens up a dialog, they type the name into an Edittext, then when they press OK, it adds that item to the ListView. Whenever I open the dialog, the app crashes. Here's the logcat:

01-17 12:45:43.020: ERROR/AndroidRuntime(212): java.lang.NullPointerException
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at com.shoppinglist.ShoppingList$1.onClick(ShoppingList.java:38)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.view.View.performClick(View.java:2344)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.view.View.onTouchEvent(View.java:4133)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.widget.TextView.onTouchEvent(TextView.java:6510)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.view.View.dispatchTouchEvent(View.java:3672)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.os.Looper.loop(Looper.java:123)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at android.app.ActivityThread.main(ActivityThread.java:4203)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at java.lang.reflect.Method.invoke(Method.java:521)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
01-17 12:45:43.020: ERROR/AndroidRuntime(212): at dalvik.system.NativeStart.main(Native Method)

Here's my .java:
Code: Select all
package com.shoppinglist;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ShoppingList extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText et = (EditText) findViewById(R.id.edittext);
        ListView lv = (ListView) findViewById(android.R.id.list);
        final ArrayList<String> items = new ArrayList<String>();
        final ArrayAdapter<String> adapter;
        adapter = new ArrayAdapter<String>(
        this, android.R.layout.simple_list_item_1, items);
        lv.setAdapter(adapter);
   
        Button button1main = (Button) findViewById(R.id.add);
        button1main.setOnClickListener(new OnClickListener()  {
           @Override
           public void onClick(View v)  {
           final Dialog additem = new Dialog(ShoppingList.this);
           additem.setContentView(R.layout.maindialog);
           additem.setCancelable(true);
           
        Button button = (Button) findViewById(R.id.cancel);
        button.setOnClickListener(new OnClickListener()  {
              @Override
              public void onClick(View v)  {
                 additem.dismiss();
              }
           });
           additem.show();
           
        Button ok = (Button) findViewById(R.id.ok);
        ok.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
              items.add(et.getText().toString());
              adapter.notifyDataSetChanged();
              additem.dismiss();
              et.setText("");
        }
           
           }
        );
       }
        });
    }
}


Any help is appreciated.
cg2916
Freshman
Freshman
 
Posts: 5
Joined: Thu Dec 30, 2010 2:18 am

Top

Re: App crashes due to nullpointerexception

Postby codeHAMMER » Mon Jan 17, 2011 8:43 pm

I am assuming that the cancel button on line 38 does exist in xml. But it looks like, and I may be wrong, that you are trying to override the cancel button in the dialog, is that right?
Color Logic v1.2 Free Game
Image
User avatar
codeHAMMER
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon Oct 11, 2010 12:39 pm

Re: App crashes due to nullpointerexception

Postby cg2916 » Mon Jan 17, 2011 10:42 pm

Uhh, I guess... not very experienced in Java so IDK
cg2916
Freshman
Freshman
 
Posts: 5
Joined: Thu Dec 30, 2010 2:18 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 17 guests