Activity cannot be cast to android.support.v4.app.Fragment - android

I am trying to change my App which has a bunch of single Activities into a swipe tab app.
i want 3 activities in the swipe tabs. I used the google example wich eclipse created for me. I don't seem to get the correct way of doing that.
I have the following (relevant) code (the other stuff is in eclipse, it's too much to post here):
Main.class which is called at launch (the last inner class of it):
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
// public DummySectionFragment() {
// }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_fragment,
container, false);
return rootView;
}
}
activity_fragment.xml:
<fragment
android:id="#+id/main"
android:name="com.mikebdev.refuel.MainActivity"
class="com.mikebdev.refuel.MainActivity"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/stats"
android:name="com.mikebdev.refuel.Statistics"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<fragment
android:id="#+id/refuel"
android:name="com.mikebdev.refuel.ReFuel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
The Activities all extend FragmentActivity from android.support.v4.app.FragmentActivity
now when starting my app, i get the following error:
04-27 12:08:11.244: E/AndroidRuntime(28728): FATAL EXCEPTION: main
04-27 12:08:11.244: E/AndroidRuntime(28728): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-27 12:08:11.244: E/AndroidRuntime(28728): at com.mikebdev.refuel.Main$DummySectionFragment.onCreateView(Main.java:184)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.View.measure(View.java:15518)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.View.measure(View.java:15518)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.View.measure(View.java:15518)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-27 12:08:11.244: E/AndroidRuntime(28728): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.View.measure(View.java:15518)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.Choreographer.doFrame(Choreographer.java:532)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.os.Handler.handleCallback(Handler.java:725)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.os.Looper.loop(Looper.java:137)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-27 12:08:11.244: E/AndroidRuntime(28728): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 12:08:11.244: E/AndroidRuntime(28728): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 12:08:11.244: E/AndroidRuntime(28728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 12:08:11.244: E/AndroidRuntime(28728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 12:08:11.244: E/AndroidRuntime(28728): at dalvik.system.NativeStart.main(Native Method)
04-27 12:08:11.244: E/AndroidRuntime(28728): Caused by: java.lang.ClassCastException: com.mikebdev.refuel.MainActivity cannot be cast to android.support.v4.app.Fragment
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
04-27 12:08:11.244: E/AndroidRuntime(28728): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)

You can't have nested fragments embedded in a xml layout file. You'll have to add them at runtime.
Edit: There are countless tutorials on implementing swipeable tabs with fragments please look at them, many are self explanatory. The main points are:
create a layout file which will contain the tabs and the ViewPager
create the fragments that will be used as the tabs/pages in the ViewPager
create a FragmentPagerAdapter and assign it to the ViewPager
bind the tabs selection to the ViewPager pages.
You could use the ViewPager directly in the Activity or you could use a wrapper Fragment, the difference is that you'll need to pass getChildFragmentManager() to the FragmentPagerAdapter instead of getSupportFragmentManager().

After a while i was able to solve everything by adding a switch case statement here:
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment;
switch (position) {
case 0:
fragment = new StatisticsFragment();
break;
case 1:
fragment = new MainActivityFragment();
break;
case 2:
fragment = new DbListViewFragment();
break;
default:
fragment = new DummySectionFragment();
break;
}
return fragment;
}
and I made a copy of my activities and converted those to fragments like explained on this video:
Converting An Activity To A Fragment
now everything seems to work.

Related

Android Share action provider

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.fullstory_m, menu);
MenuItem menuItem = menu.findItem(R.id.action_share)
mShareActionProvider = (ShareActionProvider)
MenuItemCompat.getActionProvider(menuItem);
mShareActionProvider.setShareIntent(getDefaultIntent());
whats wrong with the above code, coz it gives me null pointer exception
the xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_share"
android:title="share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
/>
</menu>
The imports
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
Stack trace
04-27 21:01:42.676: E/AndroidRuntime(1908): FATAL EXCEPTION: main
04-27 21:01:42.676: E/AndroidRuntime(1908): java.lang.NullPointerException
04-27 21:01:42.676: E/AndroidRuntime(1908): at com.pitech.danny.nhl.FullStory.onCreateOptionsMenu(FullStory.java:32)
04-27 21:01:42.676: E/AndroidRuntime(1908): at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
04-27 21:01:42.676: E/AndroidRuntime(1908): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:407)
04-27 21:01:42.676: E/AndroidRuntime(1908): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:769)
04-27 21:01:42.676: E/AndroidRuntime(1908): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:201)
04-27 21:01:42.676: E/AndroidRuntime(1908): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-27 21:01:42.676: E/AndroidRuntime(1908): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-27 21:01:42.676: E/AndroidRuntime(1908): at android.view.Choreographer.doFrame(Choreographer.java:531)
04-27 21:01:42.676: E/AndroidRuntime(1908): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-27 21:01:42.676: E/AndroidRuntime(1908): at android.os.Handler.handleCallback(Handler.java:725)
04-27 21:01:42.676: E/AndroidRuntime(1908): at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 21:01:42.676: E/AndroidRuntime(1908): at android.os.Looper.loop(Looper.java:137)
04-27 21:01:42.676: E/AndroidRuntime(1908): at
Issue could be that MenuItemCompat.getActionProvider returns null in onCreateOptionsMenu.
Since your using android support library ShareActionProvider i.e., android.support.v7.widget.ShareActionProvider , Your FullStory Activity class should extend ActionBarActivity
public class FullStory extends ActionBarActivity {
I faced the same problem. I solved it by checking my imports. Please make sure your imports are correct or not.
import android.widget.ShareActionProvider;
and in your xml android:actionProviderClass="android.widget.ShareActionProvider"

Error in changeng portrait to landscape [duplicate]

I am completely lost with this one. My app works perfectly on API 2.1 on my handset and through the emulator.
I have just run it through a 2.2 emulator and I am getting a crash and this error:
04-27 20:29:41.293: ERROR/AndroidRuntime(341): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ebp/com.ebp.ebpMainMenu}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.LinearLayout
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.os.Looper.loop(Looper.java:123)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at java.lang.reflect.Method.invoke(Method.java:521)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at dalvik.system.NativeStart.main(Native Method)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.LinearLayout
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.app.Activity.setContentView(Activity.java:1622)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at com.ebp.ebpMainMenu.onCreate(ebpMainMenu.java:58)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): ... 11 more
04-27 20:29:41.293: ERROR/AndroidRuntime(341): Caused by: java.lang.reflect.InvocationTargetException
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.widget.LinearLayout.<init>(LinearLayout.java:92)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at java.lang.reflect.Constructor.constructNative(Native Method)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): ... 21 more
04-27 20:29:41.293: ERROR/AndroidRuntime(341): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.graphics.Bitmap.nativeCreate(Native Method)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.content.res.Resources.loadDrawable(Resources.java:1705)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.view.View.<init>(View.java:1850)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.view.View.<init>(View.java:1799)
04-27 20:29:41.293: ERROR/AndroidRuntime(341): at android.view.ViewGroup.<init>(ViewGroup.java:284)
In the main.xml file, here is line #2
<LinearLayout
android:id="#+id/linearLayout3"
android:background="#drawable/mainbackground"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
and here is main line 58:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget may be the real culprit here. Try using smaller image to see if it is a memory-related issue.
Check out these links for examples of similar issues: java.lang.OutOfMemoryError: bitmap size exceeds VM budget - android - how many images?
OutOfMemory exception when loading bitmap from external storage

ActionBar Tab and Master Detail Layout

I'm having an issue with the actionbar tabs and getting the master/detail layout to work.
I have the a master/detail working but I am now adding tabs, so I can select a different layout. But, it seems to crash with the error:
04-27 13:00:45.805: E/AndroidRuntime(5161): android.view.InflateException: Binary XML file line #24: Error inflating class fragment
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-27 13:00:45.805: E/AndroidRuntime(5161): at com.example.example.masterdetail_tabs.Tab1Fragment.onCreateView(Tab1Fragment.java:17)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.app.Fragment.performCreateView(Fragment.java:1695)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1255)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.app.BackStackRecord.run(BackStackRecord.java:672)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.os.Handler.handleCallback(Handler.java:725)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.os.Looper.loop(Looper.java:137)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-27 13:00:45.805: E/AndroidRuntime(5161): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 13:00:45.805: E/AndroidRuntime(5161): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 13:00:45.805: E/AndroidRuntime(5161): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 13:00:45.805: E/AndroidRuntime(5161): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 13:00:45.805: E/AndroidRuntime(5161): at dalvik.system.NativeStart.main(Native Method)
04-27 13:00:45.805: E/AndroidRuntime(5161): Caused by: java.lang.IllegalArgumentException: Binary XML file line #24: Duplicate id 0x7f060001, tag null, or parent id 0x0 with another fragment for com.example.example.masterdetail_tabs.ItemListFragment
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
04-27 13:00:45.805: E/AndroidRuntime(5161): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
I have created a simple application to recreate the issue (don't know if it's an issue but I'm using actionbarsherlock in the real app)
https://github.com/abidk/android_masterdetail_tab_issue
Does anyone have any ideas what I am doing wrong?
If anyone is wondering the solution was to replace the fragment in the *_twopane.xml with a framelayout and then programatically adding the list component.
getFragmentManager().beginTransaction()
.replace(R.id.item_list, new ItemListFragment())
.commit();

Android App getting crashed when toggling between USB modes

I am developing an Android application which involves displaying of Google Maps using fragments.
Here are the steps to reproduce the crash:
Start the application
Open the activity using fragments
Connect Data cable
Switch USB mode to Mass storage
Resume the application
Application gets crashed
the Log for the crash is as follows:
04-27 18:45:10.289: E/AndroidRuntime(19982): FATAL EXCEPTION: main
04-27 18:45:10.289: E/AndroidRuntime(19982): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.octanetech.cortes/com.octanetech.cortes.MapActivity}: java.lang.NullPointerException
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.os.Looper.loop(Looper.java:123)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-27 18:45:10.289: E/AndroidRuntime(19982): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 18:45:10.289: E/AndroidRuntime(19982): at java.lang.reflect.Method.invoke(Method.java:521)
04-27 18:45:10.289: E/AndroidRuntime(19982): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
04-27 18:45:10.289: E/AndroidRuntime(19982): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
04-27 18:45:10.289: E/AndroidRuntime(19982): at dalvik.system.NativeStart.main(Native Method)
04-27 18:45:10.289: E/AndroidRuntime(19982): Caused by: java.lang.NullPointerException
04-27 18:45:10.289: E/AndroidRuntime(19982): at com.octanetech.cortes.MapActivity.displayPlaces(MapActivity.java:644)
04-27 18:45:10.289: E/AndroidRuntime(19982): at com.octanetech.cortes.MapActivity.onCreate(MapActivity.java:158)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-27 18:45:10.289: E/AndroidRuntime(19982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-27 18:45:10.289: E/AndroidRuntime(19982): ... 11 more
04-27 18:45:10.309: W/ActivityManager(2466): Force finishing activity com.octanetech.cortes/.MapActivity
I am using Samsung Galaxy S device.
I am using static variables as global variables. I switched to this as well, but nothing helped. The entire class in which global variables are stored is getting killed. The interesting thing to note is that when I am following the same steps in the activities where there are no fragments, then my application is not getting crashed.
Try saving your fragment state.
This can be done using "onSaveInstance" of the fragment
Also you can try setRetainInstance(true) for fragments.
When you toggle between USB modes, sometimes the Activity is recreated and hence the fragment.

Android - exception when trying to make intent go to class in a new directory

I have this intent in the manifest file:
<activity
android:name=".ProductStrategyActivity"
android:label="#string/product_strategy_header" />
I get an Exception when I try to do this:
Intent myIntent = new Intent(LearnActivity.this, ProductStrategyActivity.class);
LearnActivity.this.startActivity(myIntent);
and the class is in a package that is in my src/com/app_name/content/ directory.
Could the problem be that I need to specify that directory in the manifest file?
Sorry I made a mistake in the exception. Here is the stack trace:
04-27 02:07:23.221: E/AndroidRuntime(1234): FATAL EXCEPTION: main
04-27 02:07:23.221: E/AndroidRuntime(1234): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.problemio/com.problemio.content.ProductStrategyActivity}; have you declared this activity in your AndroidManifest.xml?
04-27 02:07:23.221: E/AndroidRuntime(1234): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
04-27 02:07:23.221: E/AndroidRuntime(1234): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
04-27 02:07:23.221: E/AndroidRuntime(1234): at android.app.Activity.startActivityForResult(Activity.java:3190)
04-27 02:07:23.221: E/AndroidRuntime(1234): at android.app.Activity.startActivity(Activity.java:3297)
04-27 02:07:23.221: E/AndroidRuntime(1234): at com.problemio.LearnActivity$3.onClick(LearnActivity.java:113)
04-27 02:07:23.221: E/AndroidRuntime(1234): at android.view.View.performClick(View.java:3511)
04-27 02:07:23.221: E/AndroidRuntime(1234): at android.view.View$PerformClick.run(View.java:14105)
04-27 02:07:23.221: E/AndroidRuntime(1234): at android.os.Handler.handleCallback(Handler.java:605)
04-27 02:07:23.221: E/AndroidRuntime(1234): at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 02:07:23.221: E/AndroidRuntime(1234): at android.os.Looper.loop(Looper.java:137)
04-27 02:07:23.221: E/AndroidRuntime(1234): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-27 02:07:23.221: E/AndroidRuntime(1234): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 02:07:23.221: E/AndroidRuntime(1234): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 02:07:23.221: E/AndroidRuntime(1234): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-27 02:07:23.221: E/AndroidRuntime(1234): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-27 02:07:23.221: E/AndroidRuntime(1234): at dalvik.system.NativeStart.main(Native Method)
That line LearnActivity.java:113 is where I tell the current intent to go to the new one.
Thanks!
.ProductStrategyActivity
replace the above word in your manifeast file with below
.content.directory.ProductStrategyActivity
The android:name in the Manifest entry can either be the absolute package name or a relative namespace relative to the namespace of the application.
But if there is a problem with the Manifest entry, you wont get an NPE. Something else must be wrong.

Categories

Resources