我刚开始在eclipse上使用java编写android,我一直在youtube上做教程。我最近从其中一个教程中复制了以下代码,但是当我运行它时,它说"不幸的是(我的应用程序)已停止"我不明白为什么......
package com.example.trythisout;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Trying extends ListActivity {
ArrayList list = new ArrayList();
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trying);
Button add = (Button) findViewById(R.id.btnAdd);
Button del = (Button) findViewById(R.id.btnDelete);
adapter = new ArrayAdapter(this, R.layout.simple_list_item_multiple_choice,list);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
EditText edit = (EditText)findViewById(R.id.items);
if(edit.getText().toString().equals(""))
{
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
else
{
Toast.makeText(getApplicationContext(),"Please enter data", Toast.LENGTH_LONG).show();
}
}
};
OnClickListener listener2 = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
SparseBooleanArray checkedItemsPosition = getListView().getCheckedItemPositions();
int count = getListView().getCount();
for(int i = count; i>=0; i--)
{
if(checkedItemsPosition.get(i))
{
adapter.remove(list.get(i));
}
}
adapter.notifyDataSetChanged();
}
};
add.setOnClickListener((android.view.View.OnClickListener) listener);
del.setOnClickListener((android.view.View.OnClickListener) listener2);
del.setOnClickListener((android.view.View.OnClickListener) listener2);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.trying, menu);
return true;
}
}