我想設置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>
是什麼'MainActivity.java' 55行? – Raghunandan
btn.setOnClickListener(listener); – newbie
post'listview.xml' – Raghunandan