请您帮忙,我在尝试为 ListView 设置适配器时遇到 nullpointerexception,listview 是 lv,我正在使用 SherlockFragmentActivity,由于某种原因 listView 为空,我在 eclipse 中执行了 clear 并检查了 id 的名称xml 很多次,sd 不为空,我已经用打印输出检查过了。这是我的代码:
排行榜活动.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>
排行榜_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>
日志猫:
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)