无法实例化PlayersActivity NullPointerException错误

时间:2013-04-18 18:25:01

标签: android nullpointerexception listactivity

我的PlayersActivity课程一直很好,直到今天神秘地停止了。我觉得我的listview有问题,但我无法弄明白,因为我确实改变了任何东西,尽管我确实为另一个类制作了另一个列表视图。无法弄清楚我哪里出错了

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

   }

logcat的

`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 `

2 个答案:

答案 0 :(得分:2)

你正在打电话

 ListView list = getListView();

在应用的生命周期之外。将初始化移至onCreate()方法。 (和/或发布该行的代码,如果您的logcat正确,这实际上会导致您的应用崩溃)...

答案 1 :(得分:1)

修改

致电

ListView list; declare  as field and then
list= getListView();

之后onCreate内的

setContentView

在您的布局文件中,R.layout.playerdata应该具有ID为

的ListView
android:id="@android:id/list"

选中此项以获取参考List Activity