我收到错误java.lang.NullPointerException,我不知道该做什么或如何解决它。
我有3个片段创建了一个tabhost,而在片段b中将会有所有项目的列表视图。
MainActivity
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import tk.delacour.tabdemo.fragments.FragmentB;
import tk.delacour.tabdemo.fragments.FragmentPageAdapter;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
FragmentPageAdapter ft;
private Bundle savedInstanceState;
ListView listView;
FragmentB baseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
ft = new FragmentPageAdapter(getSupportFragmentManager());
viewPager.setAdapter(ft);
actionBar = getActionBar();
assert actionBar != null;
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("Home").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Shop").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Contact").setTabListener(this));
listView = (ListView) findViewById(R.id.itemListView);
listView.setAdapter(new Adapter(this)); //Error Here.....
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
class SingleRow {
String codeRef;
String title;
String price;
String description;
int images;
SingleRow(String codeRef, String title, String price, String description, int images) {
this.codeRef = codeRef;
this.images = images;
this.title = title;
this.price = price;
this.description = description;
}
}
class Adapter extends BaseAdapter {
ArrayList<SingleRow> arrayList;
Context context;
Adapter(Context contxt) {
context = contxt;
arrayList = new ArrayList<SingleRow>();
Resources res = contxt.getResources();
String[] codeRef = res.getStringArray(R.array.codeRef);
String[] title = res.getStringArray(R.array.title);
String[] price = res.getStringArray(R.array.price);
String[] descriptions = res.getStringArray(R.array.description);
int[] images = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7, R.drawable.pic8, R.drawable.pic9, R.drawable.pic10, R.drawable.pic11, R.drawable.pic12, R.drawable.pic13, R.drawable.pic14,};
for (int i = 0; i < 14; i++) {
arrayList.add(new SingleRow(codeRef[i], title[i], price[i], descriptions[i], images[i]));
}
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.item_layout, parent, false);
TextView title = (TextView) row.findViewById(R.id.txtTitle);
TextView price = (TextView) row.findViewById(R.id.txtPrice);
TextView description = (TextView) row.findViewById(R.id.txtDescription);
ImageView image = (ImageView) row.findViewById(R.id.imageView);
SingleRow temp = arrayList.get(position);
title.setText(temp.title);
price.setText(temp.price);
description.setText(temp.description);
image.setImageResource(temp.images);
return row;
}
}
片段A B和C
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import tk.delacour.tabdemo.R;
/**
* Created by Christian on 05/10/2014.
*/
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
}
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import tk.delacour.tabdemo.R;
/**
* Created by Christian on 05/10/2014.
*/
public class FragmentB extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_b, container, false);
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import tk.delacour.tabdemo.R;
/**
* Created by Christian on 05/10/2014.
*/
public class FragmentC extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_c, container, false);
}
}
FragmentPageAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* Created by Christian on 05/10/2014.
*/
public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
switch (arg0){
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
logcat的
10-10 21:10:58.502 19591-19591/tk.delacour.tabdemo D/AndroidRuntime﹕ Shutting down VM
10-10 21:10:58.502 19591-19591/tk.delacour.tabdemo W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x42f05500)
10-10 21:10:58.502 19591-19591/tk.delacour.tabdemo E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{tk.delacour.tabdemo/tk.delacour.tabdemo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$600(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5390)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at tk.delacour.tabdemo.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$600(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5390)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我认为listview应该是碎片 公共类FragmentB扩展了Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_b, container, false);
listview = (ListView)v.findViewById....
return v;
}
...... } 在fragmentB中创建适配器,使用listview setAdatper。