Reputation: 13
Encountering a NullPointerException while using listview. I have initialized my adapter,my listview and my lessons array aswell.
Apologies if my doubt may sound vague. I am a newbie to android and cant figure out the bug
AboutActivity.java
public class AboutActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
String[] lessons = new String[] { "Android Introduction","Android Setup/Installation","Android Hello World","Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lessons);
ListView listview = (ListView)findViewById(R.id.ListView);
listview.setAdapter(adapter);
}
fragment_about.xml
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.chatclient.AboutActivity$PlaceholderFragment" >
<ListView
android:id="@+id/ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
</ListView>
</RelativeLayout>
logcat:
05-09 01:41:59.029: E/AndroidRuntime(1579): FATAL EXCEPTION: main
05-09 01:41:59.029: E/AndroidRuntime(1579): Process: com.example.chatclient, PID: 1579
05-09 01:41:59.029: E/AndroidRuntime(1579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chatclient/com.example.chatclient.AboutActivity}: java.lang.NullPointerException
05-09 01:41:59.029: E/AndroidRuntime(1579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-09 01:41:59.029: E/AndroidRuntime(1579): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-09 01:41:59.029: E/AndroidRuntime(1579): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-09 01:41:59.029: E/AndroidRuntime(1579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-09 01:41:59.029: E/AndroidRuntime(1579): at android.os.Handler.dispatchMessage(Handler.java:102)
05-09 01:41:59.029: E/AndroidRuntime(1579): at android.os.Looper.loop(Looper.java:136)
05-09 01:41:59.029: E/AndroidRuntime(1579): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-09 01:41:59.029: E/AndroidRuntime(1579): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 01:41:59.029: E/AndroidRuntime(1579): at java.lang.reflect.Method.invoke(Method.java:515)
05-09 01:41:59.029: E/AndroidRuntime(1579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-09 01:41:59.029: E/AndroidRuntime(1579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-09 01:41:59.029: E/AndroidRuntime(1579): at dalvik.system.NativeStart.main(Native Method)
05-09 01:41:59.029: E/AndroidRuntime(1579): Caused by: java.lang.NullPointerException
05-09 01:41:59.029: E/AndroidRuntime(1579): at com.example.chatclient.AboutActivity.onCreate(AboutActivity.java:29)
05-09 01:41:59.029: E/AndroidRuntime(1579): at android.app.Activity.performCreate(Activity.java:5231)
05-09 01:41:59.029: E/AndroidRuntime(1579): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-09 01:41:59.029: E/AndroidRuntime(1579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-09 01:41:59.029: E/AndroidRuntime(1579): ... 11 more
Upvotes: 0
Views: 158
Reputation: 11978
Error:
In your Activity
, you are calling:
setContentView(R.layout.activity_about);
This displays the layout activity_about.xml
whereas you created your views inside fragment_about.xml
. It causes the NullPointerException because the system cannot find the views:
findViewById(R.id.ListView); // this 'id' (R.id.ListView) cannot be find
Solution:
Create your views inside activity_about.xml
or change the target layout as follows:
setContentView(R.layout.fragment_about);
Upvotes: 1
Reputation: 24848
Try replacing this line:
setContentView(R.layout.activity_about);
With:
setContentView(R.layout.fragment_about);
I hope this helps!
Upvotes: 0
Reputation: 47807
You should change this
setContentView(R.layout.activity_about);
to
setContentView(R.layout.fragment_about);
It's because ListView
in your fragment_about.xml
file and you'll find ListView
in activity_about.xml
file
ListView listview = (ListView)findViewById(R.id.ListView); //Cause NULL Pointer Exception
Upvotes: 3
Reputation: 10274
you are getting null pointer because you are using placeholder i.e fragment for UI and writing java code in activity. below are the solutions.
1.setContentView(R.layout.fragment_about);
2. change your activity like below:
public class AboutActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
ArrayAdapter<String> adapter;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
String[] lessons = new String[] { "Android Introduction","Android Setup/Installation","Android Hello World","Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android"};
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,lessons);
ListView listview = (ListView)rootView.findViewById(R.id.ListView);
listview.setAdapter(adapter);
return rootView;
}
}
}
Upvotes: 0