1

下面的代码适用于我的 viewpager 适配器。

    import android.content.Context;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    import com.androidquery.AQuery;

    public class ViewPagerAdapter2 extends PagerAdapter {

    Context context;
    String[] image1;
    String[] description;


    LayoutInflater inflater;

    public ViewPagerAdapter2(Context context, String[] image1,String[] description) {
        this.context = context;
        this.image1 = image1;
        this.description=description;


    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {


        TextView des;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.slider, container,
                false);
        des = (TextView) itemView.findViewById(R.id.slider_des);
        des.setText(description[position]);
         AQuery aq = new AQuery(itemView);
         aq.id(R.id.imageView1).image(image1[position]);

         ((ViewPager) container).removeView(itemView);



        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((LinearLayout) object);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return description.length;

    }

}

我的片段的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
                R.layout.home, container, false);

        new getFromWebService().execute();
        viewPager1 = (ViewPager) mLinearLayout.findViewById(R.id.pager);
        adapter1= new ViewPagerAdapter2(mLinearLayout.getContext(),image,title);
        viewPager1.setAdapter(adapter1);

        //ListView list = (ListView) mLinearLayout.findViewById(R.id.listView1);
        //Onadapter listadapter = new Onadapter(getActivity(),id,label,title,image,hrs,posted_date);
        //list.setAdapter(listadapter);

        return mLinearLayout;

    }

我的 logcat 输出:

07-31 16:27:05.868: E/AndroidRuntime(1364): FATAL EXCEPTION: main
07-31 16:27:05.868: E/AndroidRuntime(1364): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lyfcha/com.lyfcha.TestMainActivity}: java.lang.NullPointerException
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.os.Looper.loop(Looper.java:137)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.app.ActivityThread.main(ActivityThread.java:4921)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at java.lang.reflect.Method.invoke(Method.java:511)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at dalvik.system.NativeStart.main(Native Method)
07-31 16:27:05.868: E/AndroidRuntime(1364): Caused by: java.lang.NullPointerException
07-31 16:27:05.868: E/AndroidRuntime(1364):     at com.lyfcha.ViewPagerAdapter2.getCount(ViewPagerAdapter2.java:69)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:432)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at com.lyfcha.Home1.onCreateView(Home1.java:56)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.app.Activity.performStart(Activity.java:5216)
07-31 16:27:05.868: E/AndroidRuntime(1364):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083)
07-31 16:27:05.868: E/AndroidRuntime(1364):     ... 11 more

我做了什么:1.从字符串数组中的json获取所需的数据。2. 我为 viewpager 编写了一个适配器来向它提供内容。

我遇到问题的地方:

应用程序在我的片段的 onCreateView 中抛出空指针异常,指出我的适配器的 getCount 抛出 NPE。我确信字符串数组的长度不为空(在调试透视图的显示窗口中测试)。

任何帮助将非常感激。

4

1 回答 1

1

您应该从用于获取数据的 AsyncTask 中填充适配器。由于getFromWebService在不同的线程中执行,该行

adapter1= new ViewPagerAdapter2(mLinearLayout.getContext(),image,title);

仍然没有来自另一个线程的数据。因此,解决方案是移动以下行:

LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
                R.layout.home, container, false);

        viewPager1 = (ViewPager) mLinearLayout.findViewById(R.id.pager);
        adapter1= new ViewPagerAdapter2(mLinearLayout.getContext(),image,title);
        viewPager1.setAdapter(adapter1);

onPostExecute()你的 AsyncTask 的方法getFromWebService

于 2013-07-31T11:27:53.160 回答