我的 PlayersActivity 课程一直运行良好,直到今天它神秘地停止了。我认为我的列表视图有问题,但我无法弄清楚,因为我确实改变了任何东西,尽管我确实为不同的班级制作了另一个列表视图。不能完全弄清楚我哪里出错了
public class PlayersActivity extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private PlayerDbAdapter mDbHelper;
private SimpleCursorAdapter playerlist;
ListView list = getListView();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playerdata);
mDbHelper = new PlayerDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
Cursor players = mDbHelper.fetchAllPlayers();
startManagingCursor(players);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{PlayerDbAdapter.KEY_TITLE,PlayerDbAdapter.KEY_BODY, PlayerDbAdapter.KEY_NUMBER, PlayerDbAdapter.KEY_TEAM};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.pPosition,R.id.pName,R.id.pNumber,R.id.pTeam};
// Now create a simple cursor adapter and set it to display
playerlist = new SimpleCursorAdapter(this, R.layout.playerinfo , players, from, to);
list.setAdapter(playerlist);
playerlist.notifyDataSetChanged();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
String playerName =
cursor.getString(cursor.getColumnIndexOrThrow("Player_Name"));
Toast.makeText(getApplicationContext(),
playerName, Toast.LENGTH_SHORT).show();
}
});
EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
playerlist.getFilter().filter(s.toString());
}
});
playerlist.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return mDbHelper.fetchPlayersByName(constraint.toString());
}
});
}
日志猫
`E/AndroidRuntime(341): FATAL EXCEPTION: main
E/AndroidRuntime(341): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{your.dissertation.project/your.dissertation.project.PlayersActivity}: java.lang.NullPointerException
04-18 18:15:08.619: E/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)
04-18 18:15:08.619: E/AndroidRuntime(341): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
04-18 18:15:08.619: E/AndroidRuntime(341): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-18 18:15:08.619: E/AndroidRuntime(341): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
04-18 18:15:08.619: E/AndroidRuntime(341): at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 18:15:08.619: E/AndroidRuntime(341): at android.os.Looper.loop(Looper.java:123)
04-18 18:15:08.619: E/AndroidRuntime(341): at android.app.ActivityThread.main(ActivityThread.java:3647)
04-18 18:15:08.619: E/AndroidRuntime(341): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 18:15:08.619: E/AndroidRuntime(341): at java.lang.reflect.Method.invoke(Method.java:507)
04-18 18:15:08.619: E/AndroidRuntime(341): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-18 18:15:08.619: E/AndroidRuntime(341): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-18 18:15:08.619: E/AndroidRuntime(341): at dalvik.system.NativeStart.main(Native Method)
04-18 18:15:08.619: E/AndroidRuntime(341): Caused by: java.lang.NullPointerException
04-18 18:15:08.619: E/AndroidRuntime(341): at android.app.Activity.setContentView(Activity.java:1657)
04-18 18:15:08.619: E/AndroidRuntime(341): at android.app.ListActivity.ensureList(ListActivity.java:312)
04-18 18:15:08.619: E/AndroidRuntime(341): at android.app.ListActivity.getListView(ListActivity.java:297)
04-18 18:15:08.619: E/AndroidRuntime(341): at your.dissertation.project.PlayersActivity.<init>(PlayersActivity.java:35)
04-18 18:15:08.619: E/AndroidRuntime(341): at java.lang.Class.newInstanceImpl(Native Method)
04-18 18:15:08.619: E/AndroidRuntime(341): at java.lang.Class.newInstance(Class.java:1409)
04-18 18:15:08.619: E/AndroidRuntime(341): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-18 18:15:08.619: E/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)
04-18 18:15:08.619: E/AndroidRuntime(341): ... 11 more `