If I physically slide out the keyboard on my Moto Droid A855, it crashes my test app with the stack trace pasted below. I don't understand why?
Also, if I start my app with the keyboard out, my app crashes immediately on startup.
The app consists of an activity, which contains a viewflipper as the main view layout. The viewflipper contains two linearlayouts...
Stack trace:
06-10 21:10:17.652 E/AndroidRuntime( 3785): Uncaught handler: thread main exiting due to uncaught exception
06-10 21:10:17.668 E/AndroidRuntime( 3785): java.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1#447af0b8
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:667)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.app.ApplicationContext.unregisterReceiver(ApplicationContext.java:747)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:321)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.widget.ViewFlipper.onDetachedFromWindow(ViewFlipper.java:104)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.view.View.dispatchDetachedFromWindow(View.java:5835)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1076)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1570)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.view.ViewRoot.doDie(ViewRoot.java:2556)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.view.ViewRoot.die(ViewRoot.java:2526)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:218)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.view.Window$LocalWindowManager.removeViewImmediate(Window.java:436)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3498)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3599)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.app.ActivityThread.access$2300(ActivityThread.java:119)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1867)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.os.Looper.loop(Looper.java:123)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at java.lang.reflect.Method.invoke(Method.java:521)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-10 21:10:17.668 E/AndroidRuntime( 3785): at dalvik.system.NativeStart.main(Native Method)
06-10 21:10:17.684 I/Process ( 1017): Sending signal. PID: 3785 SIG: 3
EDIT: added XML layout and relevant snippets from main activity.
Entire XML layout file
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/vFlipper"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Linear Layout 1: messages and overview. -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow android:id="#+id/TableRow01" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:text="Connection info"
android:id="#+id/tvCon1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#F0F0F0"
android:textColor="#FF0000"
/>
</TableRow>
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/tvMessages"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""
/>
</ScrollView>
</LinearLayout>
<!-- Linear Layout 2: settings -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:id="#+id/TableRow03"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="hello world"
android:id="#+id/asdfasdf2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
</LinearLayout>
</ViewFlipper>
Code snippets from Main Activity:
/**
* Attempt (not currently working) to work around this bug: http://code.google.com/p/android/issues/detail?id=6191
* TODO: make it work.
*/
#Override
public void onDetachedFromWindow() {
Log.d("Dash","OnDetachedFromWindow()");
try {
super.onDetachedFromWindow();
}
catch (Exception e) {
ViewFlipper v = (ViewFlipper)findViewById(R.id.vFlipper);
if (v != null) {
Log.d("Dash","De-Bug hit. e=" + e.getMessage());
v.stopFlipping();
}
}
}
To resolve this issue you must
Define a new class called MyViewFlipper which overrides ViewFlipper (see below)
Reference that new class anywhere you would have previously referenced ViewFlipper
Define your class and layout as shown below:
New class called MyViewFlipper. Contains the following:
package com.gtosoft.dash; // change this to match your own app.
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ViewFlipper;
public class MyViewFlipper extends ViewFlipper {
public MyViewFlipper(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDetachedFromWindow() {
try{
super.onDetachedFromWindow();
}catch(Exception e) {
Log.d("MyViewFlipper","Stopped a viewflipper crash");
stopFlipping();
}
}
}
Now to use this "fixed" version of ViewFlipper you have to reference it in the xml layout. Since it is basically a "custom view", you have to fully qualify the package path to MyViewFlipper, as seen here:
<com.gtosoft.dash.MyViewFlipper
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/vFlipper"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
(insert all the other old layout code here)
</com.gtosoft.dash.MyViewFlipper>
Now the app no longer crashes on the keyboard slide event or if the app is started with slide already out. Look for this in the log:
06-11 20:08:15.811 D/MyViewFlipper( 6106): Stopped a viewflipper crash
Credit: http://code.google.com/p/android/issues/detail?id=6191
It's really difficult to help you if you don't provide some part of your code... anyway, this error is not new and there are some workarounds in order to solve it (I guess is some bug)... try to do this:
#Override
protected void onDetachedFromWindow() {
try {
super.onDetachedFromWindow();
}
catch (IllegalArgumentException e) {
stopFlipping();
}
}
This is for overriding the onDetachedFromWindow and I hope it works for you.
I know this question was asked almost two years ago, but since then a very easy fix has been implemented. Just add android:configChanges="orientation|keyboard|keyboardHidden" into each Activity call in the Manifest.
yeah I don't see this bug before.. but then again I have this in my manifest
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|navigation"
android:label="#string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Related
This is my .java code
public class MainHelp extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.help_main);
}
}
and this is the layout
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference
android:title="Commands Documentation"
android:key="#string/commands"/>
<Preference
android:title = "How To Use"
android:key="#string/howToUse"/>
<Preference
android:title="Write Us"
android:key="#string/writeUs"/>
<Preference
android:title="About"
android:key="#string/aboutSuppApp"/>
</PreferenceScreen>
When I run the program the above code works perfectly....But, when i hit home button and try to go to this Activity, I get error saying
E/AndroidRuntime( 495): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 495): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.suppapp/com.suppapp.help.MainHelp}: android.view.InflateException: Binary XML file line #10: Error inflating class prferences
E/AndroidRuntime( 495): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime( 495): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime( 495): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime( 495): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime( 495): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 495): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 495): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 495): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 495): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 495): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 495): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class prferences
E/AndroidRuntime( 495): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
E/AndroidRuntime( 495): at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
E/AndroidRuntime( 495): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
E/AndroidRuntime( 495): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
E/AndroidRuntime( 495): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:254)
E/AndroidRuntime( 495): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:253)
E/AndroidRuntime( 495): at com.suppapp.help.MainHelp.onCreate(MainHelp.java:19)
E/AndroidRuntime( 495): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 495): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
E/AndroidRuntime( 495): ... 11 more
E/AndroidRuntime( 495): Caused by: java.lang.ClassNotFoundException: android.preference.prferences in loader dalvik.system.PathClassLoader#44c06600
E/AndroidRuntime( 495): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
E/AndroidRuntime( 495): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
E/AndroidRuntime( 495): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
E/AndroidRuntime( 495): at android.preference.GenericInflater.createItem(GenericInflater.java:375)
E/AndroidRuntime( 495): at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
E/AndroidRuntime( 495): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
E/AndroidRuntime( 495): ... 19 more
I tried to link my activity with the other .xml file but getting the same error again!! Even Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class prferences shows the same line number(#10) for other .xml files
i changed the version code(inside manifest file) and re-installed the app...its working fine now.
So I'm trying to put a LinearLayout that has two TextViews inside of a ListView but I'm having trouble and the program keeps crashing with what I've tried. The ListView would just have one element. The xml for my Linear Layout is as follows:
<ListView>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:drawablePadding="14dip"
android:paddingLeft="15dip"
android:paddingRight="15dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
</ListView>
How do I get this inside of a listview? When I try putting it in a listview, I get an error on the phone saying Sorry! The application has stopped unexpectedly. Please try again. The stacktrace is below.
I/dalvikvm( 1692): Debugger thread not active, ignoring DDM send (t=0x41504e4d l=38)
I/dalvikvm( 1692): Debugger thread not active, ignoring DDM send (t=0x41504e4d l=56)
D/AndroidRuntime( 1692): Shutting down VM
W/dalvikvm( 1692): threadid=3: thread exiting with uncaught exception (group=0x4001b170)
E/AndroidRuntime( 1692): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 1692): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.acme.activeisclickable/com.acme.activeisclickable.ActiveIsClickable}: java.lang.RuntimeException: Binary XML file line #14: You must supply a layout_width attribute.
E/AndroidRuntime( 1692): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime( 1692): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime( 1692): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime( 1692): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime( 1692): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1692): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1692): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 1692): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1692): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 1692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 1692): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1692): Caused by: java.lang.RuntimeException: Binary XML file line #14: You must supply a layout_width attribute.
E/AndroidRuntime( 1692): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:438)
E/AndroidRuntime( 1692): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3463)
E/AndroidRuntime( 1692): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3543)
E/AndroidRuntime( 1692): at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1265)
E/AndroidRuntime( 1692): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1191)
E/AndroidRuntime( 1692): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:45)
E/AndroidRuntime( 1692): at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
E/AndroidRuntime( 1692): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
E/AndroidRuntime( 1692): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime( 1692): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
E/AndroidRuntime( 1692): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
E/AndroidRuntime( 1692): at android.app.Activity.setContentView(Activity.java:1622)
E/AndroidRuntime( 1692): at com.acme.activeisclickable.ActiveIsClickable.onCreate(ActiveIsClickable.java:35)
E/AndroidRuntime( 1692): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 1692): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
E/AndroidRuntime( 1692): ... 11 more
I/Process ( 1275): Sending signal. PID: 1692 SIG: 3
I/dalvikvm( 1692): threadid=7: reacting to signal 3
I/dalvikvm( 1692): Wrote stack trace to '/data/anr/traces.txt'
I assume you are trying to customize the list items. You should write your own custom adapter to do that. Try this tutorial or you can check out these videos:
Presenting your data in a ListView
Transitioning to ListActivity
Beautify your List: Get it working
Beautify your List: Rigth way to do it
DISCLOSURE: I'm the author of the above mentioned videos.
That error is painful for my eyes to decipher to be honest. but normally
you should be able to create a listview with one child element.
what I'm saying is:
<ListView>
<LinLayout>
<TextView/>
<TextView/>
</LinLayout>
</ListView>
this should work, since ListView only allows one child. But your LinearLayout can have as many as you want. If there's an error, it might be your layoutinflater.
you have Listview not ListView (large V) in you layout file ....
E/AndroidRuntime( 1662): Caused by: java.lang.ClassNotFoundException: android.view.Listview
You have to use the ListView with a ListAdatper.
Create a class that implements ListAdapter, once of the method that you need to implement is:
public abstract View getView (int position, View convertView, ViewGroup parent);
With that method you will return a view for each of the elements of your list, and specify the layout that you want.
Also you should remember to reuse those views.
For more information about how to use properly ListViews and ListAdapters:
http://dl.google.com/googleio/2010/android-world-of-listview-android.pdf
or the video
http://www.google.com/events/io/2010/sessions/world-of-listview-android.html
Cheers,
Francisco.
You can try this
final View layout = getLayoutInflater().inflate(R.layout.add_layout, null, false);
TextView vr_name = lLayout.findViewById(R.id.name);
vr_name.setText(v_name);
linearListView.addView(layout);
Started getting a ClassCastException in a widget I've been working on that I'm not sure what it's having an issue with. I haven't modified the configure class, nor the configure layout, yet I've started getting a stack trace leading back to my configure class after modifying the main layout
E/AndroidRuntime( 2010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.metatroid.minimal.music/
com.metatroid.minimal.music.Configure}: java.lang.ClassCastException: android.widget.RadioButton
E/AndroidRuntime( 2010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime( 2010): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 2010): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime( 2010): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 2010): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2010): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2010): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 2010): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2010): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 2010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 2010): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2010): Caused by: java.lang.ClassCastException: android.widget.RadioButton
E/AndroidRuntime( 2010): at com.metatroid.minimal.music.Configure.onCreate(Configure.java:39)
E/AndroidRuntime( 2010): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 2010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime( 2010): ... 11 more
Reverting my changes causes the issue to go away, but I'm not doing anything with this file, nor am I modifying any RadioButtons...so what the hell? The line #39 of Configure.java is
EditText prv = (EditText) findViewById(R.id.previous_input);
I don't see how that ties into anything. And the only changes I am making to the entire project are changing some TextViews into Buttons in my main.xml layout which is not referenced in Configure.java. Reverting the Button back to TextView makes the error go away. What causes a "ClassCastException" and what could possibly be going on here?
Try clean your project. Previously you had RadioButton.
every time I call getResources in my main activity it results in an error and the app is forced to quit. This also happens if I call getApplicationContext().getResources() and even in a totally new project in Eclips. Do I need to do anything before the call? Error on AVD with Android 2.1 and LG GW620 with OpenEtna (2.1)
MainActivity.java:
package com.robin.blatest;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
public class MainActivity extends Activity {
Resources res = this.getResources();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.robin.blatest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
LogCat:
W/dalvikvm( 229): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime( 229): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 229): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.robin.blatest/com.robin.blatest.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime( 229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime( 229): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime( 229): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime( 229): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime( 229): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 229): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 229): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 229): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 229): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 229): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 229): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 229): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
E/AndroidRuntime( 229): at com.robin.blatest.MainActivity.<init>(MainActivity.java:9)
E/AndroidRuntime( 229): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 229): at java.lang.Class.newInstance(Class.java:1479)
E/AndroidRuntime( 229): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime( 229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
E/AndroidRuntime( 229): ... 11 more
Cheers Robin
Edit: Added Code
You can't do
Resources res = this.getResources();
in the field declaration. It's null at that point!
Put it in onCreate instead.
You'd do something like:
private Resources res = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
res = this.getResources();
}
If you wanted a field containing that data.
But it's just as easy to do this.getResources() whenever you need it.
The getResources()-method is a method from the Context-class.
When in an Activity, you can use this to access it.
But you should show us some of your code.
In an Android app, I've got an activity that is somewhat complex and may have threads running at certain times which, when finished, will update the UI. As such, having the Activity destroy and create again (due to orientation change) in the middle of one of these threads could lead to the user having to re-try that action, which is bad.
As such, I've decided to handle orientation changes myself by having android:configChanges="orientation|keyboardHidden" in the manifest, and can then override the Activity.onConfigurationChanged(Configuration newConfig) method.
The root layout for this activity is a RelativeLayout.
Currently, I don't actually do anything in onConfigurationChanged() other than call the super().
After an orientation change, the layout is muddled - some elements which are android:layout_above= are either at the bottom of the screen, or floating way above what they should be floating above.
Does anyone have any ideas as to how to fix this?
I'm aware that if I allowed the activity to restart upon an orientation change, it would then use a layout from res/layout-land/, but restarting the activity really seems to be the wrong way to go here.
Edit:
I've tried to setContentView(R.layout.displaymap); in the onConfigurationChanged() method and get the following error:
I/WindowManager( 571): onOrientationChanged, rotation changed to 1
D/StatusBar( 571): updateResources
E/AndroidRuntime( 3813): android.view.InflateException: Binary XML file line #17: Error inflating class java.lang.reflect.Constructor
E/AndroidRuntime( 3813): at android.view.LayoutInflater.createView(LayoutInflater.java:512)
E/AndroidRuntime( 3813): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:564)
E/AndroidRuntime( 3813): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
E/AndroidRuntime( 3813): at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
E/AndroidRuntime( 3813): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
E/AndroidRuntime( 3813): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime( 3813): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
E/AndroidRuntime( 3813): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
E/AndroidRuntime( 3813): at android.app.Activity.setContentView(Activity.java:1626)
E/AndroidRuntime( 3813): at com.apps.virtualtravel.DisplayMap.onConfigurationChanged(DisplayMap.java:1065)
E/AndroidRuntime( 3813): at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:3383)
E/AndroidRuntime( 3813): at android.app.ActivityThread.handleConfigurationChanged(ActivityThread.java:3449)
E/AndroidRuntime( 3813): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1762)
E/AndroidRuntime( 3813): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3813): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 3813): at android.app.ActivityThread.main(ActivityThread.java:3948)
E/AndroidRuntime( 3813): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3813): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 3813): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
E/AndroidRuntime( 3813): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
E/AndroidRuntime( 3813): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3813): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime( 3813): at com.google.android.maps.MapView.<init>(MapView.java:237)
E/AndroidRuntime( 3813): at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime( 3813): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
E/AndroidRuntime( 3813): at android.view.LayoutInflater.createView(LayoutInflater.java:499)
E/AndroidRuntime( 3813): ... 20 more
E/AndroidRuntime( 3813): Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity
E/AndroidRuntime( 3813): at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:180)
E/AndroidRuntime( 3813): at com.google.android.maps.MapView.<init>(MapView.java:279)
E/AndroidRuntime( 3813): at com.google.android.maps.MapView.<init>(MapView.java:254)
E/AndroidRuntime( 3813): ... 24 more
You have to create layout for landscape mode if you want to set for landscape and put the layout in layout-land folder with same name which is for portrait and than setContentview(R.layout.name) in onConfigChanged method. this will give the desired UI.other wise you will face issue in RelativeLayout.