Hulk Hulk - 6 months ago 11
Android Question

NullPointerException is thrown when trying to set an Adapter in Java/Android

(I already posted a question about this problem that got deleted. I am sorry if I didn't explained my problem good enough. Also I am a german person so forgive me if my english isn't very good.)

I really tried a lot of things to get this simple program to work. I think that the solution to this will be very simple but first you need to find it...
I tried three different ways to code this and ended up with this one based on this Tutorial:


  1. Define the Variable

    String[] list = {"One", "Two", "Three"};

  2. Creating the Adapter

    ListAdapter Adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

  3. Cast the ListView into the View

    ListView mainList1 = (ListView) findViewById(R.id.mainList1);

  4. Set the Adapter (wish causes the problem)

    mainList1.setAdapter(Adapter1);



So everytime I am trying to set the Adapter the Program throws out a
java.lang.NullPointerException
. As I already said I tried a lot of things to get this program to work. This is why I now need the help of the StackOverflow-Comunity. I am trying to explain this problem as good as I can so maybe one day I can finish my project...

These information's might also be relevant to this:

Full
MainActivity.java
(Original):

public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

String[] Vertretung1 = {
"1. Stunde | Frie [Ku] --› Eshk [Et Raum 008]",
"2. Stunde | Frie [Ku] --› Eshk [Et Raum 008]",
"3. Stunde | Frie [Ku] --› Eshk [Et Raum 008]"
};

String[] Vertretung2 = {
"1. Stunde | Grok [Ma] --› Grot [De Raum 111]",
"2. Stunde | Grok [Ma] --› Grot [De Raum 111]",
"3. Stunde | Grok [Ma] --› Grot [De Raum 111]"
};

ListAdapter Adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Vertretung1);

ListView mainList1 = (ListView) findViewById(R.id.mainList1);

mainList1.setAdapter(Adapter1); //Causes the Problem

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}



return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {

private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}


}

public static class FragmentOne extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_one, container, false);


return rootView;
}

}

public static class FragmentTwo extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_two, container, false);


return rootView;
}

}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
default:
return null;
}
}



@Override
public int getCount() {
return 2;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "HEUTE";
case 1:
return "MORGEN";
}
return null;
}
}
}


Full
activity_main.xml
(Original):

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="me.muehl.cvovertretungsplan.MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">

</android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

Full `fragment_main_one.xml` *(Original)*: [link][2]


Thanks for reading ;)

Answer

You must move all ListView things into the Fragment class instead of the activity since that is where you've defined the ListView in the XML.

ListView mainList1 = (ListView) findViewById(R.id.mainList1);

That line will search only in the layout you've used in setContentView.


In order to move the ListView in the Fragment, you can find this line

View view = inflater.inflate... 

And get the ListView with the next lines containing...

ListView lv = view.findViewById(...);
// TODO: setAdapter 
return view;
Comments