我是android开发的菜鸟,我试图在viewpager中显示一个listview。但是,当我尝试运行应用程序时,它在 onCreate 期间由于空指针异常而崩溃。此行导致异常:
listView.setAdapter(new UserItemAdapter(MyPagerActivity.this, R.layout.listitem, tweets));
我在这里使用了教程,所以我不明白为什么我会遇到这个问题。我是否正确实例化了列表视图?任何帮助是极大的赞赏。
public class MyPagerActivity extends Activity {
///////////////////////
ArrayList<Tweet> tweets;
ProgressDialog dialog;
Handler handler;
ImageLoader tango;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mypagermain);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
//////////////////////
tango = new ImageLoader(MyPagerActivity.this);
tweets = new ArrayList<Tweet>();
try {
JSONObject jArray = JSONfunctions.getJSONfromURL("http://search.twitter.com/search.json?q=gold%20silver%20news&rpp=15");
JSONArray parsedTwitter = jArray.getJSONArray("results");
if(parsedTwitter.equals("") ){
Toast.makeText(MyPagerActivity.this, "No twitter news", Toast.LENGTH_LONG).show();
}else{
for (int i = 0; i < parsedTwitter.length(); i++) {
JSONObject parsedspecific = parsedTwitter.getJSONObject(i);
if (i == 0){
String from_user = parsedspecific.getString("from_user");
String text = parsedspecific.getString("text");
String profile_image_url = parsedspecific.getString("profile_image_url");
Tweet tweet0 = new Tweet(from_user, text, profile_image_url );
tweets.add(tweet0);
}
}//<--for close
}//<--else close
} catch (Exception e) {
Log.e("log_tag", "Error parsing data "+ e.toString());
}
ListView listView = (ListView) findViewById(R.id.ListViewId);
listView.setAdapter(new UserItemAdapter(MyPagerActivity.this, R.layout.listitem, tweets));
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.news;
break;
case 1:
resId = R.layout.coinshows;
break;
case 2:
resId = R.layout.twitterlist;
break;
case 3:
resId = R.layout.videos;
break;
// case 4:
// resId = R.layout.farright;
// break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
}
XML 导致 null:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/ListViewId"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MyPagerMain XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myfivepanelpager"
android:background="#234567"/>
</LinearLayout>
根据丹尼斯德鲁的回答编辑(仍然导致空指针)
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.news;
View view0 = inflater.inflate(resId, null);
WebView newsfeed = (WebView) findViewById(R.id.webViewnews);
((ViewPager) collection).addView(view0, 0);
return view0;
case 1:
resId = R.layout.coinshows;
View view1 = inflater.inflate(resId, null);
WebView coinshows = (WebView) findViewById(R.id.webViewcoinshows);
((ViewPager) collection).addView(view1, 0);
return view1;
case 2:
resId = R.layout.twitterlist;
View view2 = inflater.inflate(resId, null);
LinearLayout layout=(LinearLayout)view2.findViewById(R.id.LLtwitter);
ListView listView = (ListView) findViewById(R.id.ListViewId);
listView.setAdapter(new UserItemAdapter(MyPagerActivity.this, R.layout.listitem, tweets));
((ViewPager) collection).addView(view2, 0);
return view2;
//break;
case 3:
resId = R.layout.videos;
View view3 = inflater.inflate(resId, null);
WebView youtube = (WebView) findViewById(R.id.webViewyoutube);
((ViewPager) collection).addView(view3, 0);
return view3;
}
return resId;
}