0

我有一个自定义 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 让我以正确的方式思考。

4

3 回答 3

1

您需要删除适配器的 getView 方法中的 else 部分。视图的值应该被设置,而不管它之前是否被夸大或应该是第一次被夸大。

于 2013-08-08T14:43:17.860 回答
1

如果有一件事比为您找到解决方案更好,那就是帮助您自己解决问题。好工作!

更新:我得到它的工作!谢谢Talklol,你让我找到了正确的答案!我欠你一杯啤酒!如果您发布废话,我会给您正确的答案作为感谢!再次感谢十亿。(解决方案:我必须在片段中声明列表视图,然后使用 getactivity() 作为参数!)

于 2013-08-08T23:01:24.920 回答
1

正如 Talkol 在他的评论中指出的那样,RecipeList 可能为空。我看不到带有 idmainListView的视图,activity_main.xml因此(ListView) findViewById(R.id.mainListView)将返回 null;

于 2013-08-08T18:08:19.317 回答