Ich möchte ein viewpager mit einem Layout erstellen, die eine Listenansicht enthält, aber ich bin immer immer diese Fehlermeldung:Viewpager mit Listview innerhalb
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.Join_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
Die viewpager Werke ohne die Listenansicht in Ordnung, und die Listenansicht funktioniert ohne der Viewpager. Ich kenne das Problem, ich bin mit „setContentView“ mit einem anderen Layout, wo meine Listenansicht ist, aber ich weiß nicht, wie es zu lösen, bitte helfen Sie, hier ist der Code:
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());
}
}
Meine Dateien: My_files.jpg
Mein 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"
/>
Thank you! es hat funktioniert, jetzt versuche ich einige editTexte in meinen Fragmenten zu implementieren und sammle alle ihre Werte mit einem Knopf in einem letzten Fragment, hast du eine Idee? Ich bekomme einige NullPointerExceptions – user3891850
@ user3891850 Null-Zeiger-Ausnahmen sind trivial. Du solltest eine neue Frage stellen, definitiv wird jemand antworten, sonst werde ich antworten !! – varunkr