时间:2016-05-08 00:48:55

标签: java android listview android-fragments android-viewpager

我想创建一个viewpager,其中包含一个包含listview的布局,但我总是收到此错误:

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.myapp / com.example.myapp.Join_activity}:   java.lang.NullPointerException:尝试调用虚方法'void   android.widget.ListView.setAdapter(android.widget.ListAdapter)'上的一个   null对象引用

如果没有listview,viewpager工作正常,没有viewpager,listview工作正常。我知道问题,我使用的是“setContentView”,其中我的listview是不同的布局,但我不知道如何解决它,请帮助,这里是代码:

public class Join_activity extends AppCompatActivity {

ListView lv_zona;
ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.join_pager);//here is the problem
    //my listview is in join3.xml
    //ViewPager
    viewPager = (ViewPager) findViewById(R.id.viewpaginador);
    viewPager.setAdapter(new Join_Adapter(this));

    //ListView
    lv_zona = (ListView)findViewById(R.id.lv_zona);
    lv_zona.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,new String[]{"A1","B1","C1","D1"}));


   viewPager.addOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
        @Override
        public void onPageSelected(int position) {}
        @Override
        public void onPageScrollStateChanged(int state){}
    });

}//end oncreate


}//end class

*

public class Join_Adapter extends PagerAdapter {

private Context mContext;
public Join_Adapter(Context context) {
    mContext = context;
}

@Override
public Object instantiateItem(ViewGroup coleccion, int posicion) {
    ModelObject modelObject = ModelObject.values()[posicion];
    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup mlayout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), coleccion, false);
    coleccion.addView(mlayout);
    return mlayout;
}

@Override
public void destroyItem(ViewGroup collection, int position, Object view)   
{collection.removeView((View) view);}

@Override
public int getCount() 
{return ModelObject.values().length;}

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

@Override
public CharSequence getPageTitle(int position) {
    ModelObject customPagerEnum = ModelObject.values()[position];
    return mContext.getString(customPagerEnum.getTitleResId());
}
}

我的档案: My_files.jpg

我的xmls:

***join3.xml - Here is my ListView***
<?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">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lv_zona"
            android:choiceMode="singleChoice" />


    </LinearLayout> </LinearLayout>

*

***join_pager.xml - my main layout***
<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"
tools:context=".Paginador">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView2" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpaginador"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

2 个答案:

答案 0 :(得分:0)

你应该使用片段。您正在使用视图寻呼机。现在,您应该在要在活动中加载的片段中显示列表。 This答案显示了如何做到这一点。您的片段xml将包含listView。并且适配器的getItem将返回片段。

答案 1 :(得分:0)

NullPointerException的原因是列表活动不在主布局中,因此,findViewById返回null。

要将列表视图放入ViewPager,请参阅:https://stackoverflow.com/a/12535150/189961