Using ProgressDialog when finish() on Activity, NullPointerException - java

I have a simple background task to sign up, displaying a progress dialog for the user. The issue is that I believe I'm getting NullPointerException is because the second activity that's being launched in the intent is somehow being called before the dialogProgressHide(); method, even though that one is listed first in the code, therefore different context and the progress is returning null causing error.
book.saveInBackground(new SaveCallback()
{
#Override
public void done(ParseException e) {
if (e == null)
{
dialogProgressHide();
Toast.makeText(getApplicationContext(), "Your book was posted correctly!", Toast.LENGTH_LONG).show();
finish();
}
else
{
Toast.makeText(getApplicationContext(), "An error has occured: " + e + "\n" + "Please try again!", Toast.LENGTH_LONG).show();
dialogProgressHide();
}
}
});
I could be wrong but that makes sense to me, question is how would I fix this? I simply want the dialog to disappear before finishing the activity, so that it doesn't attempt to when the progress is no longer in context.
Here is dialog methods if needed:
private void dialogProgressShow() {
mDialog = new ProgressDialog(this);
mDialog.setMessage("One moment please while we post your book.");
mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mDialog.show();
}
private void dialogProgressHide() {
if (mDialog.isShowing())
{
mDialog.dismiss();
mDialog = null;
}
}
}
LOGCAT:
03-25 17:12:44.862: E/AndroidRuntime(16528): FATAL EXCEPTION: main
03-25 17:12:44.862: E/AndroidRuntime(16528): Process: com.jameswilson.booksale, PID: 16528
03-25 17:12:44.862: E/AndroidRuntime(16528): java.lang.NullPointerException
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.jameswilson.booksale.PostBookActivity.dialogProgressHide(PostBookActivity.java:255)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.jameswilson.booksale.PostBookActivity.access$1(PostBookActivity.java:254)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.jameswilson.booksale.PostBookActivity$2$1.done(PostBookActivity.java:144)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.parse.SaveCallback.internalDone(SaveCallback.java:39)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.parse.SaveCallback.internalDone(SaveCallback.java:27)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.parse.Parse$6$1.run(Parse.java:846)
03-25 17:12:44.862: E/AndroidRuntime(16528): at android.os.Handler.handleCallback(Handler.java:733)
03-25 17:12:44.862: E/AndroidRuntime(16528): at android.os.Handler.dispatchMessage(Handler.java:95)
03-25 17:12:44.862: E/AndroidRuntime(16528): at android.os.Looper.loop(Looper.java:136)
03-25 17:12:44.862: E/AndroidRuntime(16528): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-25 17:12:44.862: E/AndroidRuntime(16528): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 17:12:44.862: E/AndroidRuntime(16528): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-25 17:12:44.862: E/AndroidRuntime(16528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-25 17:12:44.862: E/AndroidRuntime(16528): at dalvik.system.NativeStart.main(Native Method)

Check if your dialog reference is null before trying to dismiss it
private void dialogProgressHide() {
if (mDialog != null && mDialog.isShowing())
{
mDialog.dismiss();
mDialog = null;
}
}

Related

Passing Integer Values Between Activities - Android

Other stuck posts unfortunately couldn't help me.
When I clicked button while easy radiobutton is checked, the app stops working. I couldn't go and see another activity.
Sender Side:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(radiobutton_arm_triceps_easy.isChecked()) {
String dene = "my example test";
int myValue=2;
Intent intent = new Intent(getApplicationContext(), exercise_arm_triceps_execute.class);
intent.putExtra("attempt1", myValue );
startActivity(intent);
}
}
});
Receiver Side:
int receiveValue=getIntent().getIntExtra("attempt1",0);
textshow.setText(receiveValue);
LOGCAT
04-26 16:52:06.320 31527-31527/com.example.kerem.tutorial_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kerem.tutorial_project, PID: 31527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kerem.tutorial_project/com.example.kerem.tutorial_project.exercise_arm_triceps_execute}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.kerem.tutorial_project.exercise_arm_triceps_execute.onCreate(exercise_arm_triceps_execute.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Use
textshow.setText(String.valueOf(receiveValue));

Fragment cannot be cast to android.content.Context

I want to connect my login page to MySQL PHP but I got some error here.
This is my logcat:
807/com.aeu.mlibrary.mlibraryaeu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aeu.mlibrary.mlibraryaeu, PID: 1807
java.lang.ClassCastException: com.aeu.mlibrary.mlibraryaeu.LoginFragment cannot be cast to android.content.Context
at com.kosalgeek.asynctask.PostResponseAsyncTask.<init>(PostResponseAsyncTask.java:284)
at com.aeu.mlibrary.mlibraryaeu.LoginFragment.onClick(LoginFragment.java:82)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
and this is the error line in my loginFragment.java:
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("mobile", "android");
postData.put("txtUsername", etUsername.getText().toString());
postData.put("txtPassword", etPassword.getText().toString());
PostResponseAsyncTask task = new PostResponseAsyncTask(LoginFragment.this, postData);
task.execute("http://10.0.3.2/mlibrary/HTML/login.php");
}
I need your help guys!
Thank you.
Replace LoginFragment.this with getActvity()
PostResponseAsyncTask task = new PostResponseAsyncTask(getActivity(), postData);
Replace LoginFragment.this with getContext() :
PostResponseAsyncTask task = new PostResponseAsyncTask(getContext(), postData);

my android app error unfortunately app has stopped

I am new to Android.
I've just started to build my first Android app and have been struggling with this errorfor the past 2 days.
Finding no solution I had to come here.
Error:
Unfortunately "app name" has stopped
This is main activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
This is another layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
logcat error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.MainActivity}: java.lang.NullPointerException
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.os.Looper.loop(Looper.java:137)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 12:01:29.772: E/AndroidRuntime(1314): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:01:29.772: E/AndroidRuntime(1314): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 12:01:29.772: E/AndroidRuntime(1314): at dalvik.system.NativeStart.main(Native Method)
03-25 12:01:29.772: E/AndroidRuntime(1314): Caused by: java.lang.NullPointerException
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.example.listview.MainActivity.onCreate(MainActivity.java:36)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.Activity.performCreate(Activity.java:5243)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-25 12:01:29.772: E/AndroidRuntime(1314): ... 11 more
03-25 12:01:35.622: I/Process(1314): Sending signal. PID: 1314 SIG: 9
03-25 12:11:56.522: D/AndroidRuntime(1378): Shutting down VM
03-25 12:11:56.522: W/dalvikvm(1378): threadid=1: thread exiting with uncaught exception (group=0xb3adbb90)
03-25 12:11:56.542: E/AndroidRuntime(1378): FATAL EXCEPTION: main
03-25 12:11:56.542: E/AndroidRuntime(1378): Process: com.example.listview, PID: 1378
03-25 12:11:56.542: E/AndroidRuntime(1378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.MainActivity}: java.lang.NullPointerException
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.os.Looper.loop(Looper.java:137)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 12:11:56.542: E/AndroidRuntime(1378): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:11:56.542: E/AndroidRuntime(1378): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 12:11:56.542: E/AndroidRuntime(1378): at dalvik.system.NativeStart.main(Native Method)
03-25 12:11:56.542: E/AndroidRuntime(1378): Caused by: java.lang.NullPointerException
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.example.listview.MainActivity.onCreate(MainActivity.java:36)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.Activity.performCreate(Activity.java:5243)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-25 12:11:56.542: E/AndroidRuntime(1378): ... 11 more
main activity code
package com.example.listview;
import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lst = (ListView)findViewById(R.id.listView1);
//ListView mainListView = null;
String[] test = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(test) );
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );
// Set the ArrayAdapter as the ListView's adapter.
lst.setAdapter( listAdapter );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Your listview is not contained in the activity's layout so when you call it it gives you a null pointer exception. you could put the listview inside the main activity's layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="TextView" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
Line 36 is:
lst.setAdapter( listAdapter );
Means that lst is probably null.
ListView lst = (ListView)findViewById(R.id.listView1); //NULL
Failed to find the view, because you load main_activity layout, which does not have a ListView with id=listView1.
Probably you need to load another layout, which it contains.
ListView lst = (ListView)findViewById(R.id.listView1); return null because you don't use the right content in setContentView(R.layout.activity_main);. You have two options:
Use a include to put a layout inside another:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<TextView
... />
<include android:layout="#layout\my_layout_listview" />
</RelativeLayout>
Or set the other layout which contains your ListView in:
setContentView(R.layout.my_layout_listview);

NULLPOINT EXCEPTION ERROR during Simple Android app development [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
i am trying to develop this simple app using Android ADT and I got null point exception error. It is returning null point exception error at line 32 which is
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
display.setText("your total is" + counter);
}
});
My whole java code is
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.*;
public class MainActivity extends ActionBarActivity {
int counter;
Button add , subtract;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.badd1);
subtract = (Button) findViewById(R.id.bsub1);
display = (TextView) findViewById(R.id.textView1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
display.setText("your total is" + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
display.setText("your total is" + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MY xml CODE IS
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.newboston.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="your total is 0"
android:textSize="45dp" />
<Button
android:id="#+id/badd1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="34dp"
android:layout_centerHorizontal="true"
android:text="add one"
android:textSize="20dp" />
<Button
android:id="#+id/bsub1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/badd1"
android:layout_below="#+id/badd1"
android:layout_marginTop="34dp"
android:text="subtract one"
android:textSize="20dp" />
</RelativeLayout>
My logCat file is
03-25 23:10:29.219: D/AndroidRuntime(1211): Shutting down VM
03-25 23:10:29.229: W/dalvikvm(1211): threadid=1: thread exiting with uncaught exception (group=0xb2b10ba8)
03-25 23:10:29.279: E/AndroidRuntime(1211): FATAL EXCEPTION: main
03-25 23:10:29.279: E/AndroidRuntime(1211): Process: com.example.newboston, PID: 1211
03-25 23:10:29.279: E/AndroidRuntime(1211): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newboston/com.example.newboston.MainActivity}: java.lang.NullPointerException
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.os.Looper.loop(Looper.java:136)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-25 23:10:29.279: E/AndroidRuntime(1211): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 23:10:29.279: E/AndroidRuntime(1211): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 23:10:29.279: E/AndroidRuntime(1211): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-25 23:10:29.279: E/AndroidRuntime(1211): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-25 23:10:29.279: E/AndroidRuntime(1211): at dalvik.system.NativeStart.main(Native Method)
03-25 23:10:29.279: E/AndroidRuntime(1211): Caused by: java.lang.NullPointerException
03-25 23:10:29.279: E/AndroidRuntime(1211): at com.example.newboston.MainActivity.onCreate(MainActivity.java:32)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.Activity.performCreate(Activity.java:5231)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 23:10:29.279: E/AndroidRuntime(1211): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-25 23:10:29.279: E/AndroidRuntime(1211): ... 11 more
03-25 23:10:36.449: I/Process(1211): Sending signal. PID: 1211 SIG: 9
If you're using more than one layout file (e.g layout-land, layout-sw360dp), make sure all elements you reference exist on all of them.

Thread stopping issue android

I want to stop a running thread while click on the splash screen, if I don't click on screen, after the thread execution, it will launch another Activity. But getting UnSupportedException, how do I solve it?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
iImage = (ImageView) findViewById(R.id.iIcon);
splashImage = (ImageView) findViewById(R.id.splash_image);
iImage.setOnClickListener(this);
splashImage.setOnClickListener(this);
}
#Override
protected void onStart() {
super.onStart();
splashTimer = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
startActivity(new Intent(SplashActivity.this, LoginAuthenticationActivity.class));
finish();
}
});
splashTimer.start();
}
#Override
public void onClick(View view) {
if(splashTimer.isAlive())
splashTimer.stop();
switch (view.getId()) {
case R.id.iIcon:
startActivity(new Intent(this, AboutUsActivity.class));
break;
case R.id.splash_image:
startActivity(new Intent(this, LoginAuthenticationActivity.class));
break;
default:
break;
}
finish();
}
Log:
01-27 03:27:01.189: W/dalvikvm(1080): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-27 03:27:01.209: E/AndroidRuntime(1080): FATAL EXCEPTION: main
01-27 03:27:01.209: E/AndroidRuntime(1080): java.lang.UnsupportedOperationException
01-27 03:27:01.209: E/AndroidRuntime(1080): at java.lang.Thread.stop(Thread.java:1076)
01-27 03:27:01.209: E/AndroidRuntime(1080): at java.lang.Thread.stop(Thread.java:1063)
01-27 03:27:01.209: E/AndroidRuntime(1080): at com.app.wooqer.SplashActivity.onClick(SplashActivity.java:48)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.view.View.performClick(View.java:3511)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.view.View$PerformClick.run(View.java:14105)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.os.Handler.handleCallback(Handler.java:605)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.os.Handler.dispatchMessage(Handler.java:92)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.os.Looper.loop(Looper.java:137)
01-27 03:27:01.209: E/AndroidRuntime(1080): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-27 03:27:01.209: E/AndroidRuntime(1080): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 03:27:01.209: E/AndroidRuntime(1080): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 03:27:01.209: E/AndroidRuntime(1080): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-27 03:27:01.209: E/AndroidRuntime(1080): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-27 03:27:01.209: E/AndroidRuntime(1080): at dalvik.system.NativeStart.main(Native Method)
What you're doing is very wasteful (any splash screen is wasteful, but using Threads like this is more so), but to fix your issue:
use interrrupt(); intead of stop();
As the docs say for stop()
Throws UnsupportedOperationException.
And to fix the duplicate issue, move the startActivity() inside the try so it looks like this:
public void run() {
try {
Thread.sleep(5000);
startActivity(new Intent(SplashActivity.this, LoginAuthenticationActivity.class));
} catch (InterruptedException e) {
e.printStackTrace();
}
finish();
}
That way when you call interrupt() all your Activity does is finish() and the duplicate startActivity() is not called.
To further explain:
Very first issue: stop() throws an exception by default, since it's an unsafe method which you're not supposed to use.
Then when you used interrupt(), you had startActivity() in the run method after the catch block. When you interrupted, startActivity() was called once in run() and once in onClick(). By moving startActivity() inside the try block to right after Thread.sleep(), when interrupt() interrupts the Thread, the rest of the try block isn't executed. This means that you now only have one startActivity() call instead of two. For more information, read up on exceptions.

Categories