Nullpointer exception in listview adapter - android

I am using arrayadapter to show data on listview.What i have coded is shown below bt i am getting null pointer exception .Please tell me what i have missed ?
ArrayAdapter<String> adapter;
setContentView(R.layout.menu_home_activity);
OptionsList = (ListView) findViewById(R.id.OptionsLists);
String[] array = getResources().getStringArray(R.array.personal_settings);
adapter = new ArrayAdapter<String>(MenuHomeActivity.this,
R.layout.menu_home_activity, R.id.text, array);
OptionsList.setAdapter(adapter);
menu_home_activity.xml
<?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" >
<ListView
android:name="#+id/OptionsLists"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Logcat says :
03-14 10:54:59.242: E/AndroidRuntime(267): Uncaught handler: thread main exiting due to uncaught exception
03-14 10:54:59.252: E/AndroidRuntime(267): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.biz.elife/com.biz.elife.MenuHomeActivity}: java.lang.NullPointerException
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.os.Looper.loop(Looper.java:123)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-14 10:54:59.252: E/AndroidRuntime(267): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 10:54:59.252: E/AndroidRuntime(267): at java.lang.reflect.Method.invoke(Method.java:521)
03-14 10:54:59.252: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-14 10:54:59.252: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-14 10:54:59.252: E/AndroidRuntime(267): at dalvik.system.NativeStart.main(Native Method)
03-14 10:54:59.252: E/AndroidRuntime(267): Caused by: java.lang.NullPointerException
03-14 10:54:59.252: E/AndroidRuntime(267): at com.biz.elife.MenuHomeActivity.onCreate(MenuHomeActivity.java:33)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-14 10:54:59.252: E/AndroidRuntime(267): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-14 10:54:59.252: E/AndroidRuntime(267): ... 11 more
Edit :
33rd line is
OptionsList.setAdapter(adapter);
when i try setting adapter to list it gives error if i remove this line no error is displayed and also no components on screen are displayed only a black screen appears
Thanks everybody for precious time and replys my mistake was in the menu_home_activity i added android:name="#+id/OptionsList"
that i changed to
android:id="#+id/OptionsList"

Try to put the code in try-catch block
I think
OptionsList = (ListView) findViewById(R.id.OptionsLists);
is causing a null pointer. Better handle the code for possible exceptions(null or others).

I think the below code will help you to solve your problem:
public class ArrayAdapterDemo extends ListActivity {
TextView selection;
String[] items = { "this", "is", "a", "really",
"silly", "list" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_expandable_list_item_1,
items));
selection=(TextView)findViewById(R.id.selection);
}

Just replace this
adapter = new ArrayAdapter<String>(MenuHomeActivity.this,
R.layout.menu_home_activity, R.id.text, array);
in your code by
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);

Problem is in 33rd Line of your java file for the concerned Activity. I guess your list view is not getting initialized properly .

your optionsList is not getting created properly, which is probably because of some problem with ure xml file. The ListView is not getting inflated properly;
try replacing match_parent with fill_parent in android:layout_width="match_parent" and android:layout_height="match_parent"
Hope this helps.

Related

Selecting an item in ListFragment list programmatically

I've made my Android app tablet optimized and I followed the tutorial here:
Everything is great, but I am trying to select an item (say, the 1st item) with a button in the ActionBar.
I tried this answer to use performItemClick but on I've got error reports of java.lang.IllegalStateException
in android.support.v4.app.ListFragment.ensureList, java.lang.IllegalStateException: Content view not yet created, and java.lang.NullPointerException
in android.content.ComponentName.<init>
I've tried checking if the ListView is null and still get the error reports on the Play Store. How do I properly select an item in my list programmatically?
Update to add logcat and the code is virtually identical to the tutorials in the links:
Logcat A:
java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at com.ccwilcox.meteorshower.MeteorList.showMeteorDetails(MeteorList.java:69)
at com.ccwilcox.meteorshower.MeteorList.onListItemClick(MeteorList.java:62)
at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
at com.ccwilcox.meteorshower.MainActivity.viewUpcomingEvent(MainActivity.java:648)
at com.ccwilcox.meteorshower.MainActivity.onOptionsItemSelected(MainActivity.java:534)
at android.app.Activity.onMenuItemSelected(Activity.java:2606)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1045)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:592)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:149)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17273)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Logcat B:
java.lang.NullPointerException
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:2874)
at com.ccwilcox.meteorshower.MeteorList.showMeteorDetails(MeteorList.java:86)
at com.ccwilcox.meteorshower.MeteorList.onListItemClick(MeteorList.java:62)
at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3701)
at com.ccwilcox.meteorshower.MainActivity.viewUpcomingEvent(MainActivity.java:648)
at com.ccwilcox.meteorshower.MainActivity.onOptionsItemSelected(MainActivity.java:534)
at android.app.Activity.onMenuItemSelected(Activity.java:2205)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:779)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:861)
at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
at android.view.View$PerformClick.run(View.java:9152)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Update 2
And here is the code that is causing the problem:
if (mListFragment.listView != null) {
mListFragment.listView.performItemClick(mListFragment.listView.getAdapter().getView(position, null, null), position, mListFragment.listView.getAdapter().getItemId(position));
}

NullPointerException when creating Linear Layout

My code is giving me a NullPointerException and I've read through posts suggesting a clean-and-rebuild but that approach hasn't helped me so far.
My stack trace is as follows:
E/AndroidRuntime(553): FATAL EXCEPTION: main
E/AndroidRuntime(553): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.touchlogger/com.example.touchlogger.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(553): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime(553): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime(553): at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime(553): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime(553): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(553): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(553): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(553): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(553): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(553): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(553): Caused by: java.lang.NullPointerException
E/AndroidRuntime(553): at android.view.ViewGroup.addViewInner(ViewGroup.java:3336)
E/AndroidRuntime(553): at android.view.ViewGroup.addView(ViewGroup.java:3208)
E/AndroidRuntime(553): at android.view.ViewGroup.addView(ViewGroup.java:3188)
E/AndroidRuntime(553): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
E/AndroidRuntime(553): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260)
E/AndroidRuntime(553): at android.app.Activity.setContentView(Activity.java:1855)
E/AndroidRuntime(553): at com.example.touchlogger.MainActivity.onCreate(MainActivity.java:30)
E/AndroidRuntime(553): at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(553): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(553): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
My XML file is as follows:
<com.example.touchlogger.DrawView
android:id="#+id/drawView1"
android:layout_width="wrap_content"
android:layout_height="357dp" />
<Button
android:id="#+id/button1"
android:layout_width="160dp"
android:layout_height="49dp"
android:layout_gravity="center|center_vertical"
android:layout_marginBottom="29dp"
android:text="End" />
</LinearLayout>
and the line which is causing the error is:
setContentView((LinearLayout)(findViewById(R.id.layout)));
I'm new to Android programming and even if the problem is quite simple, I'm afraid it just doesn't make any sense to me at all. I've spent over two days on this and I'm completely and utterly lost. My code was working up until I decided to include a button with the drawView. That's where it got a little screwed up. I've only been having problems ever since.
Any help would be hugely appreciated! Thanks in advance!
It won't work because your are calling the findViewById() method before the setContentView() one, so it will search for a view that it hasn't created yet. Use this instead :
setContentView(R.layout.my_xml_file);
It looks like you're trying to combine two incompatible steps. The first step is to tell the system which XML file to inflate and display, so you want to do what others have suggested:
setContentView(R.layout.xml_layout_filename);
Then, the second step to get the button or DrawView or other control, use something like
Button button = (Button)findViewById(R.id.button1);
DrawView drawView = (DrawView)findViewById(R.id.drawView1);
This should be:
setContentView(R.layout.your_xml_file_name_here);

Error: Alarm (Unable to start receiver)

Please advice what the problem is with my code...
I am constantly getting this exception any suggestion would be really appreciated.
STACK_TRACE=java.lang.RuntimeException: Unable to start receiver com.platinumapps.platinumtasks.AlarmReceiver: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:1975)
at android.app.ActivityThread.access$1400(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1074)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.platinumapps.platinumtasks.AlarmReceiver.onReceive(AlarmReceiver.java:109)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:1968)
... 10 more
java.lang.NullPointerException
at com.platinumapps.platinumtasks.AlarmReceiver.onReceive(AlarmReceiver.java:109)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:1968)
at android.app.ActivityThread.access$1400(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1074)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Its a nullpointer exception.
Means that something you are calling isn't initiated correctly, Or that you dont have access to that part(ei. havn't decalred activities in AndroidManifest. Or havn't included the rights jars etc.)
If you want better help, post the code you are working with.

RuntimeException: Error inflating class fragment

I am getting a runtimeException with the following code, does anyone know why?
public class FragsApplicationActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
SuggestionFrags class
public class SuggestionFrags extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return super.onCreateView(inflater, container, savedInstanceState);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.samplefragsapplication.SuggestionFrags" />
</LinearLayout>
I am getting this exception:
11-23 18:09:23.899: E/AndroidRuntime(2436): FATAL EXCEPTION: main
11-23 18:09:23.899: E/AndroidRuntime(2436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.samplefragsapplication/com.samplefragsapplication.FragsApplicationActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.os.Looper.loop(Looper.java:123)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.reflect.Method.invoke(Method.java:521)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-23 18:09:23.899: E/AndroidRuntime(2436): at dalvik.system.NativeStart.main(Native Method)
11-23 18:09:23.899: E/AndroidRuntime(2436): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.Activity.setContentView(Activity.java:1647)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.samplefragsapplication.FragsApplicationActivity.onCreate(FragsApplicationActivity.java:12)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-23 18:09:23.899: E/AndroidRuntime(2436): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.samplefragsapplication-2.apk]
11-23 18:09:23.899: E/AndroidRuntime(2436): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
11-23 18:09:23.899: E/AndroidRuntime(2436): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
11-23 18:09:23.899: E/AndroidRuntime(2436): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
11-23 18:09:23.899: E/AndroidRuntime(2436): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
If you are going to use Fragments you need to extend FragmentActivity not Activity.
To fix this issue you have to extends from FragmentActivity instead of Activity in the FragsApplicationActivity.
The proper form will be
<fragment
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.samplefragsapplication.SuggestionFrags" />
UPD: judging by the logcat output, you don't have fragments available. If you're running a pre-Honeycomb android (but >= 1.6), you can use the compatibility library. Fragments were introduced in 3.0.
The exception android.view.InflateException: Binary XML file line: #... Error inflating class fragment might happen if you manipulate with getActivity() inside your fragment before onActivityCreated() get called. In such case you receive a wrong activity reference and can't rely on that.
For instance the next pattern is wrong:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
final View view = inflater.inflate(R.layout..., container, false);
Button button = getActivity().findViewById(R.id...);
button.setOnClickListener(...); - another problem: button is null
return view;
}
I just had the same issue, and what I found was that in my main.xml I was incorrectly referencing the fragment class.
Instead of:
<fragment android:id="#+id/frag_capt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.company.test.applicationname.fragmentname" />
try:
<fragment android:id="#+id/frag_capt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.company.test.DetailFrag" />
This fixed the issue for me.
Good luck.
Try this
<fragment
android:id="#+id/fragment1"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
Make sure android:name="className" is the first parameter in the list
ex
<fragment
android:name="com.name.first"
android:id="#+id/"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
I had the same issue, to summarise:
Extend from FragmentActivity
Follow this structure in your layout
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.maps.MapFragment"
/>

NPE when a TabActivity is started

I have an activity that is formed of 2 tabs. The problem is, when I have this code:
tabHost.setCurrentTab(0);
I am getting the following exception:
03-14 01:06:39.520: ERROR/AndroidRuntime(588): java.lang.NullPointerException
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.os.Looper.loop(Looper.java:123)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at java.lang.reflect.Method.invoke(Method.java:521)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-14 01:06:39.520: ERROR/AndroidRuntime(588): at dalvik.system.NativeStart.main(Native Method)
However, if I change the second tab to be displayed by default (I have only 2 tabs), as in:
tabHost.setCurrentTab(1);
everything is displayed correctly and I am able to switch between the tabs as well. This tells me that the tabs are configured correctly somehow, but then, why this behavior ?
I realized what I was doing wrong: the Tab mechanism was working ok, it was a ListActivity implemented for the first tab that was causing the NPE.

Categories

Resources