在SherlockFragmentActivity上为ListView设置适配器时出现nullPointerException

时间:2013-11-06 19:59:21

标签: android listview android-listview nullpointerexception

请你帮忙,我在尝试为ListView设置适配器时得到nullpointerexception,listview是lv,我使用SherlockFragmentActivity,由于某种原因listView为null,我在eclipse中执行了清除并检查了名称多次在xml中的id,sd不为null,我已经使用了打印输出来检查它。这是我的代码:

LeaderboardActivity.java:

public class LeaderboardActivity extends SherlockFragmentActivity{

private int mMenuID = R.menu.leaderboard;
ArrayList<HashMap<String,String>> playerList;
ListView lv;
SimpleAdapter sd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_leaderboard);

    // enable up button in action bar
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

         lv = (ListView) findViewById(R.id.leaderboardListView);
        new LeaderboardList().execute(this);

        sd=new SimpleAdapter(this,playerList,R.layout.leaderboard_row,new String[]{"name","email","score"},new int[]{R.id.playerNameTextView,R.id.badgeTextView,R.id.scoreTextView});
        lv.setAdapter(sd);
}

activity_leaderboard.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LeaderboardActivity" ><!-- Mijail (04-11-2013), leaderboard -->

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
    android:id="@+id/leaderboardListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>
</LinearLayout>
</RelativeLayout>

leaderboard_row.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/badgeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <TextView
        android:id="@+id/playerNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <TextView
        android:id="@+id/scoreTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>
</RelativeLayout>

logcat:

    11-06 19:43:37.814: E/AndroidRuntime(7639): FATAL EXCEPTION: main
    11-06 19:43:37.814: E/AndroidRuntime(7639): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.ifgi.sitcom.campusmapper/de.ifgi.sitcom.campusmapper.activities.LeaderboardActivity}: java.lang.NullPointerException
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.os.Handler.dispatchMessage(Handler.java:99)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.os.Looper.loop(Looper.java:137)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at java.lang.reflect.Method.invokeNative(Native Method)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at java.lang.reflect.Method.invoke(Method.java:525)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:112)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at dalvik.system.NativeStart.main(Native Method)
    11-06 19:43:37.814: E/AndroidRuntime(7639): Caused by: java.lang.NullPointerException
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.widget.ListView.setAdapter(ListView.java:463)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at de.ifgi.sitcom.campusmapper.activities.LeaderboardActivity.onCreate(LeaderboardActivity.java:71)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.app.Activity.performCreate(Activity.java:5133)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    11-06 19:43:37.814: E/AndroidRuntime(7639):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

1 个答案:

答案 0 :(得分:1)

您从未初始化playerList

据推测,一个实例是在调用new LeaderboardList().execute(this);

之后产生的