我有一个自定义 Listview,我想尝试在我的 SherlockFragment(Tab1) 中实现它。
在正常的扩展活动中,它工作得很好,但是每当我尝试在我的 SherlockFragmentActivity(MainActivity) 中声明它时 - 我都会收到NullPointerException。
我的 MainActivity 看起来像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
RecipeList = (ListView) findViewById(R.id.mainListView);
adupter = new MainListCustomBaseAdapter(ShoppingItems, this);
RecipeList.setAdapter(adupter);
//Deploying a listener menu
registerForContextMenu(RecipeList);
{
我的 FragmentTab1:
public class FragmentTab1 extends SherlockFragment {
@Override
public SherlockFragmentActivity getSherlockActivity() {
return super.getSherlockActivity();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.scanlist, container, false);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
}
还有我的customlistviewAdapter:
public class MainListCustomBaseAdapter extends BaseAdapter {
static ArrayList<ListItems> DataSomething;
static Context Cont;
public MainListCustomBaseAdapter (ArrayList<ListItems> shoppingItems, Context c){
DataSomething = shoppingItems;
Cont = c;
}
public int getCount() {
// TODO Auto-generated method stub
return DataSomething.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return DataSomething.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null){
LayoutInflater vi = (LayoutInflater)Cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainlistlayout, null);
}
else{
return v;
}
ImageView image = (ImageView) v.findViewById(R.id.ListImage);
TextView titleView = (TextView)v.findViewById(R.id.title);
ListItems msg = DataSomething.get(position);
image.setImageResource(msg.icon);
titleView.setText(msg.title);
return v;
}
public void updateResults(ArrayList<MainListCustomBaseAdapter> results){
notifyDataSetChanged();
}
}
错误如下所示:
08-08 16:25:45.655: E/AndroidRuntime(2621): FATAL EXCEPTION: main
08-08 16:25:45.655: E/AndroidRuntime(2621): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.com.barcoder/org.example.com.barcoder.MainActivity}: java.lang.NullPointerException
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread.access$600(ActivityThread.java:140)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.os.Looper.loop(Looper.java:137)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread.main(ActivityThread.java:4898)
08-08 16:25:45.655: E/AndroidRuntime(2621): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 16:25:45.655: E/AndroidRuntime(2621): at java.lang.reflect.Method.invoke(Method.java:511)
08-08 16:25:45.655: E/AndroidRuntime(2621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-08 16:25:45.655: E/AndroidRuntime(2621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-08 16:25:45.655: E/AndroidRuntime(2621): at dalvik.system.NativeStart.main(Native Method)
08-08 16:25:45.655: E/AndroidRuntime(2621): Caused by: java.lang.NullPointerException
08-08 16:25:45.655: E/AndroidRuntime(2621): at org.example.com.barcoder.MainActivity.onCreate(MainActivity.java:74)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.Activity.performCreate(Activity.java:5206)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
08-08 16:25:45.655: E/AndroidRuntime(2621): ... 11 more
任何帮助是极大的赞赏。我无法弄清楚..为了我的生活。
activity_main.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" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
FragmentTab1 - scanlist.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" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
编辑: 这件事是固定的!谢谢你,Talklol 让我以正确的方式思考。