I have been struggling with the Android 3.0 SDK's fragments concept for a few days now, and thought I'd try StackOverflow....
I have a simple (proof on concept) app:
Here is my main layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:id="#+id/relativeLayout1" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="200dp">
<fragment android:name="com.owentech.simplefragmentswap.staticfragment"
android:id="#+id/fragment1" android:layout_weight="1"
android:layout_height="wrap_content" android:layout_width="fill_parent"/>
</RelativeLayout>
<FrameLayout android:id="#+id/frameLayout1" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_width="match_parent" android:layout_above="#+id/relativeLayout1"></FrameLayout>
</RelativeLayout>
I display one fragment (staticfragment) directly in the XML, this works fine.
With the framelayout I wanted to add an XML fragment (fragment1) on create.
Here is fragment1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_height="wrap_content" android:id="#+id/button1" android:layout_alignParentTop="true" android:layout_width="match_parent" android:text="Fragment 1"></Button>
</RelativeLayout>
Here is my mainactivity:
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class mainActivity extends FragmentActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create new fragment and transaction
Fragment newFragment = new fragment1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
//transaction.replace(R.id.frameLayout1, newFragment);
transaction.add(R.id.frameLayout1, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
This causes a force close on start.
Here is the logcat:
05-18 09:49:23.073: INFO/ActivityManager(71): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.owentech.simplefragmentswap/.mainActivity }
05-18 09:49:23.313: INFO/ActivityManager(71): Start proc com.owentech.simplefragmentswap for activity com.owentech.simplefragmentswap/.mainActivity: pid=451 uid=10036 gids={1015}
05-18 09:49:23.324: DEBUG/AndroidRuntime(445): Shutting down VM
05-18 09:49:23.333: DEBUG/dalvikvm(445): Debugger has detached; object registry had 1 entries
05-18 09:49:23.404: INFO/AndroidRuntime(445): NOTE: attach of thread 'Binder Thread #3' failed
05-18 09:49:24.433: DEBUG/AndroidRuntime(451): Shutting down VM
05-18 09:49:24.433: WARN/dalvikvm(451): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): FATAL EXCEPTION: main
**05-18 09:49:24.473: ERROR/AndroidRuntime(451): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.owentech.simplefragmentswap/com.owentech.simplefragmentswap.mainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.**
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.os.Looper.loop(Looper.java:123)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at java.lang.reflect.Method.invoke(Method.java:521)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at dalvik.system.NativeStart.main(Native Method)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addView(ViewGroup.java:1865)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addView(ViewGroup.java:1822)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.view.ViewGroup.addView(ViewGroup.java:1802)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:743)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:933)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:916)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1587)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:500)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.Activity.performStart(Activity.java:3781)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
05-18 09:49:24.473: ERROR/AndroidRuntime(451): ... 11 more
05-18 09:49:24.494: WARN/ActivityManager(71): Force finishing activity com.owentech.simplefragmentswap/.mainActivity
Does anybody know what I am doing wrong, as I have taken the code directly from developer.android.com
Thanks
Sorry, found the answer. When I inflated the view in my fragment1.java it was attached to root. changed the last flag to false, now ok.
Related
I tried alot added all libraries with no use and followed documentation to make it work any help would be appreciated
Here's what appear at my logcat:
05-18 17:26:50.480: E/dalvikvm(5818): Could not find class 'com.google.maps.android.utils.demo.ClusteringDemoActivity', referenced from method com.google.maps.android.utils.demo.MainActivity.onCreate
05-18 17:26:50.552: E/AndroidRuntime(5818): FATAL EXCEPTION: main
05-18 17:26:50.552: E/AndroidRuntime(5818): java.lang.NoClassDefFoundError: com.google.maps.android.utils.demo.ClusteringDemoActivity
05-18 17:26:50.552: E/AndroidRuntime(5818): at com.google.maps.android.utils.demo.MainActivity.onCreate(MainActivity.java:22)
05-18 17:26:50.552: E/AndroidRuntime(5818): at android.app.Activity.performCreate(Activity.java:5133)
05-18 17:26:50.552: E/AndroidRuntime(5818): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-18 17:26:50.552: E/AndroidRuntime(5818): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-18 17:26:50.552: E/AndroidRuntime(5818): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-18 17:26:50.552: E/AndroidRuntime(5818): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-18 17:26:50.552: E/AndroidRuntime(5818): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-18 17:26:50.552: E/AndroidRuntime(5818): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 17:26:50.552: E/AndroidRuntime(5818): at android.os.Looper.loop(Looper.java:137)
05-18 17:26:50.552: E/AndroidRuntime(5818): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-18 17:26:50.552: E/AndroidRuntime(5818): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 17:26:50.552: E/AndroidRuntime(5818): at java.lang.reflect.Method.invoke(Method.java:525)
05-18 17:26:50.552: E/AndroidRuntime(5818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-18 17:26:50.552: E/AndroidRuntime(5818): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-18 17:26:50.552: E/AndroidRuntime(5818): at dalvik.system.NativeStart.main(Native Method)
I want to add support for my app to the Android Foryo.
But I cannot seem to figure the problem out. I have done many research using Google and in StackOverflow. The only thing I got is that I have to use <android.support.v4.app.fragment instead of <fragment using the support library, but that is also doesn't work!
Whenever I try to run the application it crushes and I get this error in the LogCat.
android.view.InflateException: Binary XML file line #40: Error inflating class android.support.v4.app.fragment
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
<LinearLayout
android:id="#+id/settings_fragment_layout"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFffffff"
android:orientation="vertical" >
<android.support.v4.app.fragment
android:id="#+id/settings_fragment_section"
android:name="com.example.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Java
/**
*
*/
package com.example;
import android.app.TabActivity;
import android.os.Bundle;
/**
* #author
*
*/
public class MainActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
SettingsFragment.java
It's just...
package com.example;
import android.os.Bundle;
import com.nozzha.support.v4.preference.PreferenceFragment;
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_settings);
}
}
LogCat
11-22 11:19:54.141: E/AndroidRuntime(1197): FATAL EXCEPTION: main
11-22 11:19:54.141: E/AndroidRuntime(1197): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: android.view.InflateException: Binary XML file line #40: Error inflating class android.support.v4.app.fragment
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.os.Looper.loop(Looper.java:123)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.reflect.Method.invoke(Method.java:521)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-22 11:19:54.141: E/AndroidRuntime(1197): at dalvik.system.NativeStart.main(Native Method)
11-22 11:19:54.141: E/AndroidRuntime(1197): Caused by: android.view.InflateException: Binary XML file line #40: Error inflating class android.support.v4.app.fragment
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.Activity.setContentView(Activity.java:1647)
11-22 11:19:54.141: E/AndroidRuntime(1197): at com.example.MainActivity.onCreate(MainActivity.java:19)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-22 11:19:54.141: E/AndroidRuntime(1197): ... 11 more
11-22 11:19:54.141: E/AndroidRuntime(1197): Caused by: java.lang.NoSuchMethodException: fragment(Context,AttributeSet)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.Class.getMatchingConstructor(Class.java:660)
11-22 11:19:54.141: E/AndroidRuntime(1197): at java.lang.Class.getConstructor(Class.java:477)
11-22 11:19:54.141: E/AndroidRuntime(1197): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
11-22 11:19:54.141: E/AndroidRuntime(1197): ... 22 more
That is because your Activity does not extend the base Activity FragmentActivity. If you use Support Based Fragment, then your Activity must extend the FragmentActivity.
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
please try adding the constructor. public void SettingsFragment(){}. Activity needs it to properly initialize the fragment.
selectQType a simple spinner. The values are getting populated from the DB and they are fine.
ArrayAdapter<String> spinnerArrayAdapter1 =
new ArrayAdapter<String>(SpotlighterBasicActivity.this,
android.R.layout.simple_spinner_item, spinnerQTypeList);
spinnerArrayAdapter1.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
Spinner selectQType=(Spinner) findViewById(R.id.spinnerInputQType);
selectQType.setPrompt("QType");// logcat points warning on this line.
selectQType.setAdapter(spinnerArrayAdapter1);
selectQType.setSelection(QTypeIndex);
selectQType.setOnItemSelectedListener(new OnItemSelectedListener() {
...
}
This is the output i get for Tablet:
Strange part is that it is working perfectly fine for mobile category of emulators and devices, but giving warning for tablet category of emulators and devices.
Following is the logcat detail:
05-18 16:47:29.284: E/SpotlighterBasicActivity(481): QTypeIndex=0
05-18 16:47:29.284: E/SpotlighterBasicActivity(481): spinnerArrayList[1]=Type 1, spinnerQTypeID=5853, defaultQType=6307
05-18 16:47:29.294: E/SpotlighterBasicActivity(481): spinnerArrayList[2]=Type 2, spinnerQTypeID=6308, defaultQType=6307
05-18 16:47:29.304: W/System.err(481): java.lang.NullPointerException
05-18 16:47:29.304: W/System.err(481): at in.varit.spotlighter.basic.SpotlighterBasicActivity.loadQTypeSpinners(SpotlighterBasicActivity.java:334)
05-18 16:47:29.304: W/System.err(481): at in.varit.spotlighter.basic.SpotlighterBasicActivity.onCreate(SpotlighterBasicActivity.java:203)
05-18 16:47:29.304: W/System.err(481): at android.app.Activity.performCreate(Activity.java:4397)
05-18 16:47:29.304: W/System.err(481): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
05-18 16:47:29.313: W/System.err(481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
05-18 16:47:29.313: W/System.err(481): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
05-18 16:47:29.313: W/System.err(481): at android.app.ActivityThread.access$500(ActivityThread.java:122)
05-18 16:47:29.313: W/System.err(481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
05-18 16:47:29.325: W/System.err(481): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 16:47:29.325: W/System.err(481): at android.os.Looper.loop(Looper.java:132)
05-18 16:47:29.325: W/System.err(481): at android.app.ActivityThread.main(ActivityThread.java:4123)
05-18 16:47:29.325: W/System.err(481): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 16:47:29.325: W/System.err(481): at java.lang.reflect.Method.invoke(Method.java:491)
05-18 16:47:29.334: W/System.err(481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-18 16:47:29.334: W/System.err(481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-18 16:47:29.334: W/System.err(481): at dalvik.system.NativeStart.main(Native Method)
selectQType is null And
check using if(null!=selectQType)
and also check id of selectQType is it right from layout.xml?
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.org.BatteryManager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<com.org.BatteryManager.BatteryView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:textColor="#ffffffff"
/>
</LinearLayout>
Logcat
02-17 18:49:49.392: WARN/AppWidgetHostView(124): updateAppWidget couldn't find any view, using error view
02-17 18:49:49.392: WARN/AppWidgetHostView(124): android.view.InflateException: Binary XML file line #9: Error inflating class com.org.BatteryManager.BatteryView
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.widget.RemoteViews.apply(RemoteViews.java:930)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:219)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:250)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:73)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.os.Looper.loop(Looper.java:123)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at java.lang.reflect.Method.invoke(Method.java:521)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at dalvik.system.NativeStart.main(Native Method)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): Caused by: java.lang.ClassNotFoundException: com.org.BatteryManager.BatteryView in loader dalvik.system.PathClassLoader[.]
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
02-17 18:49:49.392: WARN/AppWidgetHostView(124): ... 15 more
I had the same confusing problem. To fix it your custom view should provide a constructor with two arguments: Context and AttributeSet as adviced here.
What is com.org.BatteryManager.BatteryView?
If you want to use a custom view then it is possible. The way to do it is create a custom view class in Java that extends some base view component class.
For example, I use custom Gallery components in lots of my applications. The gallery class will only move one frame left or right on a swipe which is different from the default behavior.
The way that I made my custom gallery is to extends the basic Gallery class --
package com.testing.whatever;
public class CustomGallery extends Gallery {
//CODE OVERRIDES HERE
}
Then in my XML code, the code looked similar to what you already have --
<com.testing.whatever.CustomGallery android:layout_width="fill_parent" android:layout_height="fill_parent" />
I suspect you're getting your error because you haven't coded com.org.BatteryManager.BatteryView or it's in the wrong place in your java files.
This question already has an answer here:
how to show alert inside an activity group?
(1 answer)
Closed 8 years ago.
In my activity I am calling webservices. So after the webservice returns the result, I have to show an alert. Since alert is UI part, I think inside onPostExecute() I have to write the alert code. But when I do like that error is coming.
Error shown:
12-02 09:59:08.508: ERROR/AndroidRuntime(451): Uncaught handler: thread main exiting
due to uncaught exception
12-02 09:59:08.528: ERROR/AndroidRuntime(451):
android.view.WindowManager$BadTokenException: Unable to add window -- token
android.app.LocalActivityManager$LocalActivityRecord#4378eb50 is not valid; is your
activity running?
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.view.ViewRoot.setView(ViewRoot.java:456)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.view.Window$LocalWindowManager.addView(Window.java:409)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.app.Dialog.show(Dialog.java:238)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.app.AlertDialog$Builder.show(AlertDialog.java:802)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at com.myapp.android.activities.Register$PostCodeTask.onPostExecute(Register.java:291)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at com.myapp.android.activities.Register$PostCodeTask.onPostExecute(Register.java:1)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.os.AsyncTask.finish(AsyncTask.java:416)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.os.AsyncTask.access$300(AsyncTask.java:127)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:428)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.os.Handler.dispatchMessage(Handler.java:99)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
android.os.Looper.loop(Looper.java:123)
android.app.ActivityThread.main(ActivityThread.java:4203)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
java.lang.reflect.Method.invokeNative(Native Method)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
java.lang.reflect.Method.invoke(Method.java:521)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
12-02 09:59:08.528: ERROR/AndroidRuntime(451): at dalvik.system.NativeStart.main(Native
Method)
Can anyone please help to solve my issue. Thanks in advance :)
You need to execute your alert code in UI thread. There are few ways of doing it for example
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MyActivity.this, "Hello there", Toast.LENGTH_LONG).show();
}
});