Getting crash in Android on onResume() in my activity - Why? - java

The error is:
Unable to resume activity: java.lang.NullPointerException
Here is the stack trace:
0 java.lang.RuntimeException: Unable to resume activity {com.sortitapps.movies/com.sortitapps.movies.SettingsActivity}: java.lang.NullPointerException
1 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2944)
2 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2973)
3 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2408)
4 at android.app.ActivityThread.access$600(ActivityThread.java:165)
5 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
6 at android.os.Handler.dispatchMessage(Handler.java:107)
7 at android.os.Looper.loop(Looper.java:194)
8 at android.app.ActivityThread.main(ActivityThread.java:5391)
9 at java.lang.reflect.Method.invokeNative(Native Method)
10 at java.lang.reflect.Method.invoke(Method.java:525)
11 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
13 at dalvik.system.NativeStart.main(Native Method)
14 Caused by: java.lang.NullPointerException
15 at com.sortitapps.movies.SettingsActivity.onResume(SettingsActivity.java:149)
16 at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1259)
17 at android.app.Activity.performResume(Activity.java:5200)
18 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2931)
And here is the code. Line 149 is adapter.notifyDataSetChanged();:
#Override
public void onResume()
{
super.onResume(); // Always call the superclass method first
SharedPreferences settings = getSharedPreferences("prefs", 0);
int numberDesktop = settings.getInt("desktop", 0);
Log.i("Desktop purchased:", String.valueOf(numberDesktop));
int numberScans = settings.getInt("scanning", 0);
Log.i("Number of scans:", String.valueOf(numberScans));
if (settingsitems.contains("Unlock Desktop Apps") &&
numberDesktop == 2)
{
settingsitems.remove("Unlock Desktop Apps");
}
String freeapp = getResources().getString(R.string.free_app);
if (settingsitems.contains("Unlock Premium Features") &&
freeapp.equals("Yes") &&
numberScans > 10)
{
settingsitems.remove("Unlock Premium Features");
}
adapter.notifyDataSetChanged();
}
Do I need to initialize the adapter again when coming resuming?

Most certainly adapter is null.
Your code doesn't show how the adapter got initialized, so it could be that it wasn't initialized with a value, or it was set to null after that.

Related

ArrayList just recognise the first element

I have an ArrayList that I add some data in a method, when I try to remove a item that isn't the first one I get the following error
07-25 00:39:03.024 28960-28960/murilopereira.brofut E/[HAS CHECK]: 2
07-25 00:39:03.025 28960-28960/murilopereira.brofut E/[CHECK INDEX]: udidjdid
0
84jdkd
1
07-25 00:39:07.787 28960-29062/murilopereira.brofut D/OpenGLRenderer: endAllActiveAnimators on 0x7f6b86bc00 (MenuPopupWindow$MenuDropDownListView) with handle 0x7f6b8e6940
07-25 00:39:08.054 28960-28960/murilopereira.brofut E/[HAS CHECK]: 1
07-25 00:39:08.055 28960-28960/murilopereira.brofut E/[CHECK INDEX]: 84jdkd
0
07-25 00:39:08.055 28960-28960/murilopereira.brofut D/AndroidRuntime: Shutting down VM
07-25 00:39:08.060 28960-28960/murilopereira.brofut E/AndroidRuntime: FATAL EXCEPTION: main
Process: murilopereira.brofut, PID: 28960
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at murilopereira.brofut.Times.deleteTeam(Times.java:203)
at murilopereira.brofut.Times.access$100(Times.java:29)
at murilopereira.brofut.Times$2.onClick(Times.java:109)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
But if I check the size of the array, that indicates a number uppon one
My code to delete:
public static ArrayList<String> hasKeys = new ArrayList<>();
...
private void deleteTeam(int indexRemove){
Log.e("[HAS CHECK]", hasKeys.size() + "");
for(int i = 0; i < hasKeys.size(); i++){
Log.e("[CHECK INDEX]", hasKeys.get(i));
Log.e("[CHECK INDEX]", i + "");
}
// Log.e("[CHECK INDEX]", hasKeys.indexOf(m_orders.get(indexRemove).getTeamName()) + "");
hasKeys.remove(hasKeys.indexOf(m_orders.get(indexRemove).getTeamName()) - 1);
Menu.teams.remove(m_orders.get(indexRemove).getTeamName());
m_orders.remove(indexRemove);
m_adapter.removeItem(indexRemove);
m_adapter.notifyDataSetChanged();
}
I'm using the following code to add items to it
m_orders = new ArrayList<Teams>();
Teams o1 = new Teams();
hasKeys.add(teamName.getText().toString());
Log.e("[HAS CHECK]", hasKeys.size() + "");
o1.setTeamName(teamName.getText().toString());
o1.setTeamPlayers(playerOne.getText().toString() + ", " + playerTwo.getText().toString() + ", " + playerThree.getText().toString() + ", " + playerFour.getText().toString() + ", " + playerFive.getText().toString());
Menu.teams.put(o1.getTeamName(), o1.getTeamPlayers());
m_orders.add(o1);
This is happening because your item is always added at index 0. The size of array list is 1 every time from your add items. I don't know the exact flow of your code. But it's seems like if you are calling below statement every time:
m_orders = new ArrayList<Teams>();
This is instantiating your array list.

Cannot parse a String into Float in my app [duplicate]

This question already has an answer here:
java.lang.NumberFormatException: Invalid float: "x" in android
(1 answer)
Closed 5 years ago.
At first I am new in android development. I tried to make this small app where I wanted to take inputs from the users and show them to a (default)list view. When I put values in all the fields it works fine except when I keep the CGPA field empty it stopped. Unfortunately, MyApp is stopped
Here is the OnClickListener login button.
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setData();
}
});
Here is the lines of setData() method where i guess the problem has occurred.
My question is "Is it a right approach to parse data?"
Float cgpa = Float.parseFloat(editTextCgpa.getText().toString());
if(cgpa<=4.00 && cgpa > 0.00 ){
student.setCgpa(cgpa);
}
else{
error = true;
editTextCgpa.setError("CGPA must be within 4 in scale");
}
Android Monitor:
Here is the error message that i found in android monitor.
11-25 13:06:18.917 7896-7896/com.ariful.lict.lictpractice E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NumberFormatException: Invalid float: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseFloat(StringToReal.java:289)
at java.lang.Float.parseFloat(Float.java:300)
at com.ariful.lict.lictpractice.MainActivity.setData(MainActivity.java:55)
at com.ariful.lict.lictpractice.MainActivity$1.onClick(MainActivity.java:45)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
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:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
here does the convertion of the String to Float is causing the error ??
Is there anyone who can help me?
You are passing an empty string to be parsed...
Just change your code to check for null/empty String.
if(!editTextCgpa.getText().toString().isEmpty()){
Float cgpa = Float.parseFloat(editTextCgpa.getText().toString());
if(cgpa<=4.00 && cgpa > 0.00 ){
student.setCgpa(cgpa);
}
else{
error = true;
editTextCgpa.setError("CGPA must be within 4 in scale");
}
}

OpenCV Android crashes on Imgproc.cvtColor()

I have a problem with OpenCV4Android.
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.camtime/com.fognature.develop.smp.CameraActivity}:
CvException [org.opencv.core.CvException: cv::Exception:
/hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/imgproc/src/color.cpp:3739:
error: (-215) scn == 3 || scn == 4 in function void
cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
]
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2320)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2380)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: CvException [org.opencv.core.CvException: cv::Exception:
/hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/imgproc/src/color.cpp:3739:
error: (-215) scn == 3 || scn == 4 in function void
cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
]
at org.opencv.imgproc.Imgproc.cvtColor_1(Native Method)
at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:4598)
at
com.fognature.develop.smp.CameraActivity.onCreate(CameraActivity.java:108)
at android.app.Activity.performCreate(Activity.java:6018)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2273)
... 10 more
On this code part:
Mat sec;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sec = new Mat();
setContentView(R.layout.activity_camera);
File photo = new File(Environment.getExternalStorageState()
+File.separator+"SMP"+File.separator+getIntent().getStringExtra("c_name")+File.separator+getIntent().getStringExtra("last_photo"));
sec = Highgui.imread(photo.getAbsolutePath());
Imgproc.cvtColor(sec, sec, Imgproc.COLOR_RGBA2GRAY);
}
It always crash on Imgproc.cvtColor(sec, sec, Imgproc.COLOR_RGBA2GRAY);
The error states that :
error: (-215) scn == 3 || scn == 4 in function void
cv::cvtColor(cv::InputArray, cv::OutputArray, int, int) ]
which generally means that:
When you are trying to convert RGBA2GRAY, then the input Mat sec in this case has Only 1 channel. You can confirm this by logging the sec.channels() just after reading it.
It is also possible that, input Mat was empty or corrupted, to confirm you may check sec.width, sec.height, sec.depth, etc attributes to assure that it was loaded properly.

Strange runtime exception on Android 4.4.4

I've got this strange stack trace from Flurry Analytics and I have no idea where it comes from. All the classes are from android system and not a single line that tells me where it comes from.
This error happened 3 times on a same device with android 4.4.4
Any ideas? Thanks.
java.lang.RuntimeException
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2836)
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2865)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2291)
android.app.ActivityThread.access$800(ActivityThread.java:144)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
android.os.Handler.dispatchMessage(Handler.java:102)
android.os.Looper.loop(Looper.java:212)
android.app.ActivityThread.main(ActivityThread.java:5135)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
dalvik.system.NativeStart.main(Native Method)
Caused by: android.app.ActivityThread.deliverResults(ActivityThread.java:3455)
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2823)
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2865)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2291)
android.app.ActivityThread.access$800(ActivityThread.java:144)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
android.os.Handler.dispatchMessage(Handler.java:102)
android.os.Looper.loop(Looper.java:212)
android.app.ActivityThread.main(ActivityThread.java:5135)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
dalvik.system.NativeStart.main(Native Method)
Part of the problem here seems to be that Flurry error reporting is leaving out important information.
Anyhow, this is where I think the exception is coming from:
private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
final int N = results.size();
for (int i=0; i<N; i++) {
ResultInfo ri = results.get(i);
try {
if (ri.mData != null) {
ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
}
if (DEBUG_RESULTS) Slog.v(TAG,
"Delivering result to activity " + r + " : " + ri);
r.activity.dispatchActivityResult(ri.mResultWho,
ri.mRequestCode, ri.mResultCode, ri.mData);
} catch (Exception e) {
if (!mInstrumentation.onException(r.activity, e)) {
throw new RuntimeException(
"Failure delivering result " + ri + " to activity "
+ r.intent.getComponent().toShortString()
+ ": " + e.toString(), e);
}
}
}
}
(This code isn't from Android 4.4.4, but this particular method is identical in the versions I looked at ...)
It would seem that deliverResults is catching some exception it got further up the stack, and wrapping / resthoring it as a RuntimeException. At the point that the exception is constructed, it has a message, and cause. Whatever is generating the stacktrace has removed that information, and that is going to make diagnosis hard.

Android - java.lang.ArrayIndexOutOfBoundsException

I'm new to Java, but I have done some PSP coding in C before. This seems to be quite different.
Below is the stack trace for what I'm seeing:
java.lang.RuntimeException: An error occured while executing doInBackground()
enter code hereenter code here`at android.os.AsyncTask$3.done(AsyncTask.java:299)
enter code here`at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:173)
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
And here is the patch of code:
#Override
protected Void doInBackground(Void... arg0) {
String searchQuery = title.replaceAll("[^ \\w]", "");
searchQuery = searchQuery.replace(" ", "+");
String searchResult = httpRun("http://search.azlyrics.com/search.php?q="+searchQuery).toString();
System.out.println("http://search.azlyrics.com/search.php?q="+searchQuery);
if(!searchResult.contains("Sorry, no results") && !searchResult.equals("")) {
String link = searchResult.split("<table width=100%>")[1];
link = link.substring(link.indexOf("<a href=\""), link.indexOf("\" rel"));
link = link.replace("<a href=\"", "");
String lR = httpRun(link).toString();
if(lR != "" && lR.contains("<!-- start of lyrics -->")) {
lR = lR.substring(lR.indexOf("<!-- start of lyrics -->"), lR.indexOf("<!-- end of lyrics -->"));
lR = lR.replace("<!-- start of lyrics -->", "");
lyrics = lR;
} else {
fail = 1;
lyrics = "Couldn't find lyrics";
}
} else {
fail = 1;
lyrics = "Couldn't find lyrics";
}
return null;
}
I've done some research, and have read that you can't print to the screen within doInBackground - but I'm not sure where to go from here. Any help would be appreciated.
EDIT: java.lang.StringIndexOutOfBoundsException
in java.lang.String.startEndAndLength
Heres the new stack:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.StringIndexOutOfBoundsException: length=7338; regionStart=537; regionLength=-538
at java.lang.String.startEndAndLength(String.java:593)
at java.lang.String.substring(String.java:1474)
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:174)
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
Your split call is returning an array of size 1, but you're trying to get the 2nd item in it (with the [1]). You have to account for the possibility that your split string isn't in the input.
String link = searchResult.split("<table width=100%>")[1];
The error is occuring there. Indices start at 0, and your split is returning an array of size 1, so element 1 is out of bounds, and hence the exception is thrown. If you change it to String link = searchResult.split("<table width=100%>")[0]; the exception will no longer be thrown.

Categories

Resources