I have a MapActivity with a mapview. It works fine. Now I want to add a SeekBar onto my MapView. It has to be at the bottom but on the mapview. User will scroll to set the distance. Whatever. My problem is, that any view I am trying to put there is null, when I reach them by findViewById().
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="mykey"/>
<SeekBar
android:id="#+id/DistanceBar"
android:max="2000"
android:progress="2000"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="14dip"></SeekBar>
</RelativeLayout>
I am trying to reach that like this:
public class MyActivity extends MapActivity implements LocationListener {
...
SeekBar DistanceBar;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
DistanceBar = (SeekBar) findViewById(R.id.DistanceBar); //This is afterwards null.
I have made a research everywhere and couldn't find a similar problem. Someone used something very similar. So I ignored that it is null, I didn't call the SeekBar in onCreate(). But the SeekBar hasn't been drawen at all. Can't I add other Views to a mapactivity?
Thanks for any idea!
An IMPORTANT EDIT: The graphical layout viewer shows the SeekBar as it should at the bottom of mapview.
I still get NullPointerException:
ERROR/AndroidRuntime(12738): FATAL EXCEPTION: main
ERROR/AndroidRuntime(12738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyApps.MapLocations/com.MyApps.MapLocations.MyActivity}: java.lang.NullPointerException
ERROR/AndroidRuntime(12738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
ERROR/AndroidRuntime(12738): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
ERROR/AndroidRuntime(12738): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
ERROR/AndroidRuntime(12738): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
ERROR/AndroidRuntime(12738): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(12738): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(12738): at android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(12738): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(12738): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(12738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
ERROR/AndroidRuntime(12738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
ERROR/AndroidRuntime(12738): at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(12738): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(12738): at com.MyApps.MapLocations.MyActivity.setupSeekbar(MyActivity.java:343)
ERROR/AndroidRuntime(12738): at com.MyApps.MapLocations.MyActivity.onCreate(MyActivity.java:89)
ERROR/AndroidRuntime(12738): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR/AndroidRuntime(12738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
ERROR/AndroidRuntime(12738): ... 11 more
Here is the result from HierarchyViewer:
Can't I add other Views to a mapactivity?
Yes, you can.
Clean your project (Project -> Clean from the Eclipse main menu or ant clean at the command line). Then use Hierarchy View to see what is going on. It sounds like either your IDs are off kilter (in which case the clean will help) or perhaps you failed to save the XML or something.
I just have copied the main.xml file as map.xml and deleted main.xml. And it worked. I still don't understand why it happened. Maybe the "main" causes some problem. Maybe MapActivities have defined for themselves a "main" layout which is interrupting my "main" layout. But this was the solution somehow. Thanks a lot for your patience!
IMPORTANT EDIT: I have definitely found the problem. main.xml file remained in layout-normal and layout-normal-long folders without any SeekBar. I forgot to edit those.
Related
I put text-selectable TextView in Android layout with setTextIsSelectable(true) and the text is set by Html.fromHtml(sometexts).
It works good to allow user to copy text.
My JellyBean device gives small baloons with cut/copy/paste buttons on top of the selected text.
But when user accidently click cut icon left side of the copy menu, it crashes.
The log is given as below but there is no point to handle this exception in my application.
How to prevent app crash?
01-06 19:29:16.025 2048-2048/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.text.SpannableString cannot be cast to android.text.Editable
at android.widget.TextView.deleteText_internal(TextView.java:8865)
at android.widget.TextView.onTextContextMenuItem(TextView.java:8315)
at android.widget.Editor$ActionPopupWindow.onClick(Editor.java:3556)
at android.view.View.performClick(View.java:4114)
at android.view.View$PerformClick.run(View.java:17097)
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:4885)
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:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
I'm not sure and I can't test it right now but maybe try to put that in your TextView xml :
android:bufferType="spannable"
If it's still not working you can implement yourself your action for the selectable event.
Look at the answer of this question here :
Android- How can I show text selection on textview?
The main Activity in my Android app has a Gallery widget that loads XML layouts (containing TextViews and Images) through an efficient ImageAdapter. For the most part this works fine and I haven't had any problems on any of my devices or the emulator, but I have seen the following error in my logs. It looks like this is affecting less than 1% of users, but I'd like to know what's causing it, how to resolve it, or at least "catch" it and resolve gracefully. Any ideas?
java.lang.NullPointerException at
android.widget.Gallery.setUpChild(Gallery.java:772) at android.widget.Gallery.makeAndAddView(Gallery.java:751) at android.widget.Gallery.fillToGalleryLeft(Gallery.java:667) at android.widget.Gallery.trackMotionScroll(Gallery.java:378) at android.widget.Gallery$FlingRunnable.run(Gallery.java:1369) 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:3695) 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)
I get this problem several days ago, please check your own gallery's adpater check its getView() methods,see if you return null in some if-cases,just replace return null with new View(context) (or other view but null) will simply solve this problem.Good luck
Some users are getting this error.
I use a viewgroup to continuously display a banner add.
I then use child views for the program and switch back and forth between views as the user clicks a button.
I can cause a crash by switching back and forth between child views 6 to 10 times.
Here is the layout for the viewgroup:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.company.programname"
xmlns:app="http://schemas.android.com/apk/res/com.adwhirl"
android:orientation="vertical"
android:background="#color/darkslategrey"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.adwhirl.AdWhirlLayout
android:id="#+id/adwhirl_layout"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="52dp"
/>
</LinearLayout>
Here is the code creating the ViewGroup:
setContentView(R.layout.viewgrouplayout);
llLinLay=(LinearLayout)findViewById(R.id.LinearLayout01);
liInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Here is the main child layout with irrelevent pieces cut out:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/MainLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/MainShowAllDesiredButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/AnotherButton"
android:layout_toRightOf="#id/AButton"
android:textSize="15sp"
android:text="Show All Desired"
android:onClick="MainDesired"
/>
<ListView
android:id="#+id/ItemTypeList"
android:background="#color/aqua"
android:cacheColorHint="#color/aqua"
android:fastScrollEnabled="true"
android:layout_width="fill_parent"
android:layout_height="160dip"
android:layout_below="#id/SomeItem"
/>
</RelativeLayout>
Here is an example of the code creating a view:
vAllDesired=liInflater.inflate(R.layout.alldesiredlayout,null);
vAllDesired.setId(7);
llLinLay.addView(vAllDesired);
final ListView lvAllDesired = (ListView)findViewById(R.id.AllDesiredList);
laAllDesiredAdapter = new AllDesiredListAdapter(this, alAllDesired);
lvAllCoinsDesired.setAdapter(laAllDesiredAdapter);
ViewGroup.LayoutParams AllDesiredParams = lvAllCoinsDesired.getLayoutParams();
AllDesiredParams.height = AllCoinsDesiredHeight;
lvAllCoinsDesired.setLayoutParams(AllDesiredParams);
FillAllDesiredArray();
Here is an example of the code used to switch between views:
bReturnToMainScreen = (Button) findViewById(R.id.DesiredReturnToMainScreenButton);
bReturnToMainScreen.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View V) {
llLinLay.removeView(vAllDesired);
MainWindow();
}
});
Here is the trace from Android Developer website. The line number in the program is not always the same.
java.lang.NullPointerException
at android.webkit.WebView.requestFocus(WebView.java:6113)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1029)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1073)
at android.view.ViewGroup.requestFocus(ViewGroup.java:1032)
at android.view.View.requestFocus(View.java:3559)
at android.view.ViewRoot.clearChildFocus(ViewRoot.java:1586)
at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
at android.view.ViewGroup.clearChildFocus(ViewGroup.java:508)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:2207)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:2181)
at android.view.ViewGroup.removeView(ViewGroup.java:2129)
at com.jimbobga.mycoinsus.ProgramName$8.onClick(ProgramName.java:488)
at android.view.View.performClick(View.java:2411)
at android.view.View$PerformClick.run(View.java:8819)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Just curious, when you're making your calls to findViewById, are they in the OnCreate method or directly where you're inflating the row in the adapter? As it relates to your example here, the findViewById method is only guaranteed to work in the OnCreate method or where the particular row is inflated, so its possible that calling it elsewhere could cause unexpected behavior.
Look at this line:
at com.jimbobga.mycoinsus.ProgramName$8.onClick(ProgramName.java:488)
If this is happening after orientation has changed and views have not been assigned then you have a problem.
I'm experiencing the same issue.
Take a look at this response: Issues With Adwhirl(Admob+Inmobi+..)
Eric from AdMob staff says that it's a problem with the Android framework.
Did you try to surround your OnClick() code with a: try/catch Throwable Exception?
I am setting an animation on my view from following the ApiDemo example (see layout_grid_fade.html):
<?xml version="1.0" encoding="utf-8"?>
<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:rowDelay="50%"
android:directionPriority="column"
android:animation="#anim/fade" />
and below is my code
mView.startAnimation(AnimationUtils.loadAnimation(ViewModel.this, R.anim.layout_grid_fade));
mView.setImage(modelImages.get(0).image);
but i get exception dont know why? below is my log trace
FATAL EXCEPTION: main
java.lang.RuntimeException: Unknown animation name: gridLayoutAnimation
at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:116)
at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:83)
at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:64)
at nick.kimK.ViewModel$1$1.run(ViewModel.java:72)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
at dalvik.system.NativeStart.main(Native Method)
UPD:
Not every xml in res/anim folder declares an animation. Some of them might declare Animators or LayoutAnimationControllers. Those are not Animations, thus they can't be loaded with the loadAnimation() call.
--
It looks like the gridLayoutAnimation tag describes not a particular basic animation type but rather a GridLayoutAnimationController. So it can be loaded directly with AnimationUtils.loadAnimation() but rather should be set to a ViewGroup (a layout) throuh layoutAnimation property. If you still want to obtain the AnimationController instance in code, use AnimationUtils.loadLayoutAnimation() method:
LayoutAnimationController layoutAnimation = AnimationUtils.loadLayoutAnimation(ViewModel.this, R.anim.layout_grid_fade)
But you hardly can use the layoutAnimation in the way you're doing in your example.
I found this article quite useful for understanding the layout animations.
I'm working for hours on an error I get, when I want to start a ListActivity.
Short workaround about what I want to do:
I have a main application with a normal menu, from which I want to start a setting list where the user can add specific settings.
I'm trying to start the ListView like this:
startActivity(new Intent(this, FileManagerSettings.class));
Then in the on Create method I got the error:
userSettings = deserializeObject();
if(userSettings.isEmpty())
{
userSettings.add(new SettingItem("Camera", android.os.Environment.DIRECTORY_DCIM, false));
}
super.onCreate(icicle);
Context mContext = this.getApplicationContext();
settingsList = (ListView)findViewById(R.id.list_settings);
settingsList.setAdapter(new CustomSettingsAdapter(mContext, userSettings));
The last line causes the error. For better understanding here the Constructor of the adapter class:
public CustomSettingsAdapter(Context context, ArrayList<SettingItem> sitems)
{
settingsArrayList = sitems;
mInflater = LayoutInflater.from(context);
}
And here is the error from the logcat: (Sorry, don't know how to format it right)
04-01 11:07:26.756: ERROR/AndroidRuntime(375): FATAL EXCEPTION: main
04-01 11:07:26.756: ERROR/AndroidRuntime(375): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.openintents.filemanager/org.openintents.filemanager.FileManagerSettings}: java.lang.NullPointerException
04-01 11:07:26.756: ERROR/AndroidRuntime(375): Caused by: java.lang.NullPointerException
04-01 11:07:26.756: ERROR/AndroidRuntime(375): at org.openintents.filemanager.FileManagerSettings.onCreate(FileManagerSettings.java:43)
In my thinking the error is caused because the LayoutInflater of the context is null... But I don't know how to get the LayoutInflater then and even don't know if this is really the error...
edit: error log with using setContentView():
04-01 12:06:56.315: ERROR/AndroidRuntime(669): FATAL EXCEPTION: main
04-01 12:06:56.315: ERROR/AndroidRuntime(669): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.openintents.filemanager/org.openintents.filemanager.FileManagerSettings}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
this is the xml-File:
<LinearLayout android:layout_width="fill_parent"
android:id="#+id/list"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/settings_list_title"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="#string/settings_header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center" />
<ListView android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
findViewById() only returns a valid reference to a View if the layout of the Activty has been set. Looking at the code you have pasted in to your question, it does not appear that you are making a call to
setContentView(R.layout.whatever_your_layout_file_id);
Your variable settingsList, would then be a null reference