I'm struggling to find the cause of a certain NullPointerException that's being thrown when I try to start a specific activity.
LogCat output:
12-11 13:29:28.211: E/AndroidRuntime(701): Caused by: java.lang.NullPointerException
12-11 13:29:28.211: E/AndroidRuntime(701): at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:214)
12-11 13:29:28.211: E/AndroidRuntime(701): at android.content.res.Resources.getTextArray(Resources.java:361)
12-11 13:29:28.211: E/AndroidRuntime(701): at android.content.res.TypedArray.getTextArray(TypedArray.java:628)
12-11 13:29:28.211: E/AndroidRuntime(701): at android.widget.ListView.<init>(ListView.java:168)
12-11 13:29:28.211: E/AndroidRuntime(701): at android.widget.ListView.<init>(ListView.java:159)
12-11 13:29:28.211: E/AndroidRuntime(701): ... 25 more
onCreate() method of the activity in question:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
}
XML for the layout "display" that my activity is trying to load:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
<ListView
android:id="#+id/weekdayListView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
<ListView
android:id="#+id/timeSlotListView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:entries="#layout/item_layout" >
</ListView>
</LinearLayout>
Other than what's in onCreate(), my activity doesn't do anything else. Since there's no code to populate the listviews (there was but I commented it out in trying to find the root of the problem), when the activity starts it should just be a blank screen.
When I comment out the section in the XML file relating to weekdayListView, the activity loads just fine, so I have to assume there's a problem with how I've set up the XML. The only difference I can see between the two is that I don't explicitly set the android:entries attribute, but if I understand it correctly, in this case it should just use the default.
I've been stuck on this for a while now so any help is greatly appreciated. I tried to provide as much information as I thought was relevant but if there's anything else I forgot to include, let me know.
Your android:entries attribute points to a layout when it should rather point to a string array resource.
Create a string array in your strings.xml, e.g.
<string-array name="vals">
<item>Foo</item>
<item>Bar</item>
</string-array>
and then use
android:entries="#string/vals"
Related
Crash occurs on Samsung devices (J7 and S7) running Android 7.0.
I've built a tiny test app and can reproduce the crash. Single Activity that extends Activity (not AppCompatActivity), simple layout containing a single EditText with inputType="text". Target SDK is 8.
When positioning the cursor in the EditText and typing, words are suggested and words are underlined. Click on some of the underlined words or complete editing in the EditText and then return to the EditText. After entering some data and clicking around inside the EditText, the app will crash with this stacktrace:
06-01 18:56:37.990 10339-10339/sharpmind.de.samsungtest W/ResourceType: No package identifier when getting value for resource number 0x00000000
06-01 18:56:37.991 10339-10339/sharpmind.de.samsungtest D/AndroidRuntime: Shutting down VM
06-01 18:56:37.992 10339-10339/sharpmind.de.samsungtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: sharpmind.de.samsungtest, PID: 10339
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:202)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2970)
at android.content.res.Resources.getLayout(Resources.java:1986)
at android.view.LayoutInflater.inflate(LayoutInflater.java:425)
at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
at android.widget.Editor$SuggestionsPopupWindow.initContentView(Editor.java:3704)
at android.widget.Editor$PinnedPopupWindow.<init>(Editor.java:3395)
at android.widget.Editor$SuggestionsPopupWindow.<init>(Editor.java:3683)
at android.widget.Editor.replace(Editor.java:432)
at android.widget.Editor$3.run(Editor.java:2359)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
With target SDK set to 14, the crash does not occur.
If I disable the autocorrect and word suggestions (which isn't that easy to do on a Samsung device), the crash also does not occur. But this makes my users really really angry, so it isn't a viable solution.
Activity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:minHeight="200dp"
android:inputType="text"
android:layout_height="wrap_content"/>
</RelativeLayout>
So, heres the point:
I'm training up and making my first steps in android development and after I narrowed down a few errors myself, I finally came across one that I cant find the solution for.
I want to create an app that displays two pictures that split the screen in half horizontally. So I create a LinearLayout with a vertical orientation and add two RelativeLayout's inside it. I compile it and the build apparently completes but as soon as the app tries to start up on my mobile (Samsung S5 Neo) it says it stopped working and I land up on my desktop again. >.< Also, before tips alike arise - yes I started a new project for testing purposes already and the same error occurs. Also, when I remove that lines of code, the app works just fine on my mobile. So, that's my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="removedbecausenotyourbusiness:P">
<RelativeLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
<ImageView
android:id="#+id/background2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background2"
android:scaleType="centerCrop"/>
</RelativeLayout>
<RelativeLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
<ImageView
android:id="#+id/background1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background1"
android:scaleType="centerCrop"/>
</RelativeLayout>
</LinearLayout>
Also, I'd like to get to know if I can somehow get a log from my mobile as to WHY the app stopped working. Where can I get this?
EDIT: I believe I found what you were asking me for.
12-17 19:27:18.751 11830-11830/? E/Zygote: v2 12-17 19:27:18.751
11830-11830/? E/Zygote: accessInfo : 0 12-17 19:27:20.211
11830-11830/com.example.marco E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.marco, PID: 11830
java.lang.OutOfMemoryError: Failed to allocate a 231444012 byte
allocation with 4182352 free bytes and 125MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
at
android.content.res.Resources.loadDrawableForCookie(Resources.java:4211)
at android.content.res.Resources.loadDrawable(Resources.java:4085)
at android.content.res.Resources.loadDrawable(Resources.java:3935)
at android.content.res.TypedArray.getDrawable(TypedArray.java:886)
at android.widget.ImageView.(ImageView.java:157)
at android.widget.ImageView.(ImageView.java:145)
at
android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:60)
at
android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:56)
at
android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at
android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021)
at
android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080)
at
android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:758)
at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at
android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
at
android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at
android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)
at
android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at com.example.marco.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6877)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3208)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3351)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Here is some things you can do to fix the OutOfMemoryError from your images:
Add these lines in your AndroidManifest file under the application at the top:
android:hardwareAccelerated="false"
android:largeHeap="true"
So the very top of of your AndroidManifest will look something like this with the two added lines:
<application
android:hardwareAccelerated="false" //added
android:largeHeap="true" //added
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
If that doesn't fix it, then you will need to compress your image files. You can do that on a site like https://tinyjpg.com/. It is probably just an issue of your images being too large!
Also next time you run into a problem like this, you need to use that error that you received in your crash log / stacktrace and search for the solution. In your case, you could have found this: Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM
i'm new to android studio and my app is crushing besuase line of code
songProgressBar.setOnSeekBarChangeListener(this);
posting a question here helped me to realize it is a typo with an id in my layout. Well, here is the code for my layout with songProgressBar as an id.
<SeekBar
android:id="#+id/songProgressBar"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:layout_above="#id/player_footer_bg"
android:thumb="#drawable/seek_handler"
android:progressDrawable="#drawable/seekbar_progress_blue"
android:paddingLeft="6dp"
android:paddingRight="6dp"/>
Now, i don't know what the typo is, and i don't know what i'm to do. the person who told me it is a typo said i should check whether the component is present in the layout when the activity is running. I've got no idea what I'm to do now. pls help.
There seems to be a null pointer exception.
21:39:10.999 1443-1443/? E/AndroidRuntime: Caused by: java.lang.NullPointerException 01-10 21:39:10.999 1443-1443/? E/AndroidRuntime: at com.example.passion.patrimusic.MainActivity.onCreate(MainActivity.java:86) 01-10 21:39:10.999
And from your above comment, its possible that songProgressBar is null.
If you are getting it from the layout, check if there's any typo in the Id and whether the component is present in the layout when the activity is running.
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'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