clicklistener错误召唤对话框功能

时间:2014-05-15 11:04:27

标签: android android-dialog

我正在尝试设置触发对话框的onclick侦听器,但我的代码中出现了一些错误。这是我错误地分配了听众。因为它出现了null pointerexception错误,它是怎么发生的?

这是我的logcat错误列表。

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)

这是我的代码

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

这是我的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>

2 个答案:

答案 0 :(得分:1)

您的listview.xml布局没有ID为insert的视图。

您可能想要添加

android:id="@+id/insert"

到那里的Button

自从您对代码进行了注释后,还要从XML中删除onClick属性。


根据评论进行编辑:

  

insert with my dialog.xml

然后在创建对话框时,在您膨胀的布局上调用findViewById()等,例如

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

根据dialog.xml编辑2:

insertEditText,不是Button。请下定决心:)

答案 1 :(得分:0)

您发布的insert中没有标识为listview.xml的按钮。因此btn为空,您将在空对象上调用setOnClickListener(listener)

添加

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