由于onClick上的java.lang.NullPointerException,Android App在启动时崩溃

时间:2011-01-23 19:09:53

标签: android crash

我想这样做,以便用户点击按钮来启动对话框。单击按钮时崩溃。这是logcat:

  
    

在com.shoppinglist.ShoppingList上的nullpointerexception $ 1 $ 2.onClick(ShoppingList.java:50)在android.view.View.performClick(View.java:2344)的android.view.View.onTouchEvent(View.java:4133) )android.widget.TextView.onTouchEvent(TextView.java:6510)在android.view.View.DispatchTouchEvent(View.java:3672)的android.view.ViewGroup中的android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) .ViewGroup.dispatchTouchEvent(ViewGroup.java:882)位于android.view.ViewGroup.DispatchTouchEvent(ViewGroup.java:882),位于android.view.ViewGroup.DispatchTouchEvent(ViewGroup.java:882),位于android.view.ViewGroup.dispatchTouchEvent( ViewGroup.java:882)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

  

这是我的.java文件:

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.setTitle("Type your item");
            additem.setCancelable(true);
            et.setHint("Type the name of an item...");

        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("");
        }

            }
        );
       }
        });
    }
}

这是我的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF9900"
    android:padding="10dp"
    android:orientation="vertical">
        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add item..."
            android:layout_gravity="center"/>
        <ListView android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#FFCC00"/>  
</LinearLayout>

这是我的对话框xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/layout_root"
  android:orientation="vertical"
  android:background="#FFFFFF"
  android:minHeight="100dp"
  android:minWidth="300dp">
  <EditText
     android:id="@+id/edittext"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"/>
 <LinearLayout
    android:id="@+id/button_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:layout_gravity="bottom">
  <Button
    android:id="@+id/ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ok"/>
  <Button
    android:id="@+id/cancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cancel"/>
  </LinearLayout>   
</LinearLayout>

2 个答案:

答案 0 :(得分:4)

    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);

在您的代码中,您使用setContentViewR.layout.main。然后,您尝试在R.id.edittext中找到R.layout.main,但edittext中存在maindialog.xml。 所以当你打电话时

final EditText et = (EditText) findViewById(R.id.edittext);

et将为nullet上的任何方法调用都会产生NullPointerException

修改

试试这个:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        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);
            final EditText et = (EditText)additem.findViewById(R.id.edittext);
            additem.setTitle("Type your item");
            additem.setCancelable(true);
            et.setHint("Type the name of an item...");

            Button button = (Button) additem.findViewById(R.id.cancel);
            button.setOnClickListener(new OnClickListener()  {
                @Override
                public void onClick(View v)  {
                    additem.dismiss();
                }
            });
            additem.show();

            Button ok = (Button) additem.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("");
                }
            });
       }
        });

答案 1 :(得分:1)

如果我的行号正确,则在此块中发生空指针异常:

new OnClickListener() {
            @Override
            public void onClick(View v) {
                items.add(et.getText().toString());
                adapter.notifyDataSetChanged();
                additem.dismiss();
                et.setText("");
        }

错误是说您正在访问的其中一个变量为null。在此函数中,您正在访问et,et.getText(),items,adapter和additem。我建议调试这一部分(通过eclipse或打印日志语句)来确定哪个项为空。