2014-05-15 38 views
0

我想设置onclick侦听器,将触发对话框,但在我的代码中出现了一些错误。这是我错误地指派了听众。因为它看起来具有null pointerexception错误,它是怎么发生的?错误与clicklistener召唤对话框功能

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

是什么'MainActivity.java' 55行? – Raghunandan

+0

btn.setOnClickListener(listener); – newbie

+0

post'listview.xml' – Raghunandan

回答

0

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

添加

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

listview.xml布局不具有ID insert的视图。

你可能想

android:id="@+id/insert" 

添加到Button那里。

也从XML中删除onClick属性,因为您已将代码注释掉了。


编辑基于评论:

插入与我dialog.xml

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

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

编辑2基于dialog.xml:

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

+0

insert with my dialog.xml – newbie

+0

dialog.xml发布 – newbie