簡體   English   中英

Viewpager里面有listview

[英]Viewpager with listview inside

我想創建一個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)'在空對象引用上

viewpager在沒有listview的情況下工作正常,listview在沒有viewpager的情況下工作正常。 我知道這個問題,我正在使用“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"
    />

你應該使用片段。 您正在使用視圖尋呼機。 現在,您應該在要在活動中加載的片段中顯示列表。 這個答案顯示了如何做到這一點。 您的片段xml將包含listView。 並且適配器的getItem將返回片段。

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

要將列表視圖放入ViewPager,請參閱: https ://stackoverflow.com/a/12535150/189961

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM