NullPointerException on onDestroy() method - java

I got this problem while exiting my app: when You click 'Back' button the LeadBolt ExitAd appears. Then You click 'No' and app exits and toast appears with "This app has stopped."
So, there's something with ad OR it's something with setting MediaPlayer to null.
Code looks like this:
alarm = new MediaPlayer();
...
#Override
public void onPause() {
super.onPause();
if (alarm != null) {
alarm.release();
alarm = null;
}
}
#Override
public void onStop() {
super.onStop();
if (alarm != null) {
alarm.release();
alarm = null;
}
}
public void onDestroy() {
myController.destroyAd();
// myController2.destroyAd();
if (adView != null) {
adView.destroy();
}
super.onDestroy();
if (alarm != null) {
alarm.release();
alarm = null;
}
}
Exception logs:
11-05 11:06:49.130: E/AndroidRuntime(2019): FATAL EXCEPTION: main
11-05 11:06:49.130: E/AndroidRuntime(2019): java.lang.RuntimeException: Unable to destroy activity {com.klangstudios.car_pilot_remote/com.my_company.my_app.MainActivity}: java.lang.NullPointerException
11-05 11:06:49.130: E/AndroidRuntime(2019): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3451)
11-05 11:06:49.130: E/AndroidRuntime(2019): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3469)
11-05 11:06:49.130: E/AndroidRuntime(2019): at android.app.ActivityThread.access$1200(ActivityThread.java:141)
11-05 11:06:49.130: E/AndroidRuntime(2019): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
11-05 11:06:49.130: E/AndroidRuntime(2019): at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 11:06:49.130: E/AndroidRuntime(2019): at android.os.Looper.loop(Looper.java:137)
11-05 11:06:49.130: E/AndroidRuntime(2019): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-05 11:06:49.130: E/AndroidRuntime(2019): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 11:06:49.130: E/AndroidRuntime(2019): at java.lang.reflect.Method.invoke(Method.java:511)
11-05 11:06:49.130: E/AndroidRuntime(2019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-05 11:06:49.130: E/AndroidRuntime(2019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-05 11:06:49.130: E/AndroidRuntime(2019): at dalvik.system.NativeStart.main(Native Method)
11-05 11:06:49.130: E/AndroidRuntime(2019): Caused by: java.lang.NullPointerException
11-05 11:06:49.130: E/AndroidRuntime(2019): at com.klangstudios.car_pilot_remote.MainActivity.onDestroy(MainActivity.java:209)
11-05 11:06:49.130: E/AndroidRuntime(2019): at android.app.Activity.performDestroy(Activity.java:5273)
11-05 11:06:49.130: E/AndroidRuntime(2019): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1110)
11-05 11:06:49.130: E/AndroidRuntime(2019): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3438)
11-05 11:06:49.130: E/AndroidRuntime(2019): ... 11 more
Anyone got similar problem?

In your code, you should replace the following:
myController.destroyAd();
with this:
if(myController!= null) {
myController.destroyAd();
}
then try it. You will avoid getting a null pointer exception, because if myController is null, then the program won't be destroying something that doesn't exist

Related

networkInfo.isConnected() error

I'm receiving an error when executing this code.
When connected to wifi, the function should display MAC address, otherwise "---"
private void DisplayWifiStatus() {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
if (networkInfo.isConnected()) {
textMac.setText(info.getMacAddress());
}else {
textMac.setText("-----");
}
}
Logcat/Stacktrace:
03-11 21:32:07.532 3348-3348/com.light87sakurahimegmail.wifiscanner E/dalvikvm﹕ >>>>> Normal User
03-11 21:32:07.532 3348-3348/com.light87sakurahimegmail.wifiscanner E/dalvikvm﹕ >>>>> com.light87sakurahimegmail.wifiscanner [ userId:0 | appId:10337 ]
03-11 21:32:07.782 3348-3348/com.light87sakurahimegmail.wifiscanner E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.light87sakurahimegmail.wifiscanner, PID: 3348
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.light87sakurahimegmail.wifiscanner/com.light87sakurahimegmail.wifiscanner.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.access$800(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.light87sakurahimegmail.wifiscanner.MainActivity.DisplayWifiStatus(MainActivity.java:79)
at com.light87sakurahimegmail.wifiscanner.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5389)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
            at android.app.ActivityThread.access$800(ActivityThread.java:163)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5335)
            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:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

How to store a single row data into a string array of sqlite database in android

I have called a function returnCurrentNameById() by passing the id of the particular row. But it gives an error.
String[] s1;
public String[] returnCurrentNameById(int k) throws SQLException{
String[] columns = new String[]{ KEY_ID1, KEY_NAME, Key_DOB, KEY_AGE, KEY_PHONE_NO, Key_EXPERIENCE, KEY_EMAIL, KEY_STATUS, KEY_STATUS};
SQLiteDatabase db3 = this.getReadableDatabase();
Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + "=" + k, null, null, null, null);
if (c != null){
c.moveToFirst();
for(int i = 0; i<=8; i++){
s1[i] = c.getString(i);
}
return s1;
}
return null;
}
The Log cat error report is:
02-15 16:38:13.424 1545-1545/world.com.my`enter code here`progect09 D/AndroidRuntime﹕ Shutting down VM
02-15 16:38:13.464 1545-1545/world.com.myprogect09 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x2bc9a300)
02-15 16:38:13.533 1545-1545/world.com.myprogect09 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3591)
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)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
            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)
Caused by: java.lang.NullPointerException
at world.com.myprogect09.DataBaseSQL.returnCurrentNameById1(DataBaseSQL.java:209)
at world.com.myprogect09.ThirdActivity.isClicked(ThirdActivity.java:74)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3586)
            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)
02-15 16:38:34.491 1545-1545/world.com.myprogect09 I/Process﹕ Sending signal. PID: 1545 SIG: 9
You have not initialized your s1 array.
Use
s1 = new String[9];
to allocate an array of 9 elements.
Also check the return value of moveToFirst() so you don't get an exception in case the query returns no rows.
Replace
Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + "=" + k, null, null, null, null);
with
Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + " = ?", new String[]{String.valueOf(k)}, null, null, null);

parse android upload and download

i am using the services of parse.com, android guide:
https://www.parse.com/docs/android_guide
and it didnt work for me (i get a message: app must be closed...)
here is my code:
gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
try {
gameScore.save();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gameScore.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
// Access the object id here
objectId= gameScore.getObjectId();
}
});
ParseQuery query = new ParseQuery("GameScore");
query.getInBackground(objectId, new GetCallback() {
#Override
public void done(ParseObject parseObject, ParseException arg1) {
// TODO Auto-generated method stub
int score= parseObject.getInt("score");
Integer i =new Integer(score);
t1.setText(i.toString());
}
});
and here is the Logcat:
02-28 09:59:10.552: E/AndroidRuntime(773): FATAL EXCEPTION: main
02-28 09:59:10.552: E/AndroidRuntime(773): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.salebook/com.example.salebook.MainActivity}: java.lang.NullPointerException
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.os.Looper.loop(Looper.java:137)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-28 09:59:10.552: E/AndroidRuntime(773): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 09:59:10.552: E/AndroidRuntime(773): at java.lang.reflect.Method.invoke(Method.java:511)
02-28 09:59:10.552: E/AndroidRuntime(773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-28 09:59:10.552: E/AndroidRuntime(773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-28 09:59:10.552: E/AndroidRuntime(773): at dalvik.system.NativeStart.main(Native Method)
02-28 09:59:10.552: E/AndroidRuntime(773): Caused by: java.lang.NullPointerException
02-28 09:59:10.552: E/AndroidRuntime(773): at com.example.salebook.MainActivity.onCreate(MainActivity.java:69)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.Activity.performCreate(Activity.java:5104)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-28 09:59:10.552: E/AndroidRuntime(773): ... 11 more
Also, when i am on debugging mode, i see that the objectId is null ,although i can see in the server that the object uploded succesfully with problem and it has a valid objectId.
Also, when i look in the gameScore object in eclipse i can see that it has the same objectId as in the server , however again it return me null in the objectId
thanks
So what is happening is you are executing saveInBackground which starts on a new thread. You will then immediately call query.getInBackground with a null objectId. You need to wait and call query.getInBackground AFTER the done call in gameScore.saveInBackground.
I would recommend moving the second call to the done function call in the gamerScore.saveInBackground execution above. Also you must consider that the save may fail so you will want to catch the exception as follows:
public void done(List<ParseObject> items, ParseException e) {
if (e == null) {
//execute query here
} else {
//handle exception here
}
}

NullPointerException on TabGroupactivity when setting button typeface

I'm having a little problem with an activity I have loading in a TabGroupActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_profile);
f = new Functions();
String j = f.getUserInfo();
arialFont = Typeface.createFromAsset(this.getAssets(),"Arial.ttf");
username = (TextView)findViewById(R.id.txtProfileName);
firstname = (TextView)findViewById(R.id.txtName);
Address1 = (TextView)findViewById(R.id.txtAdd1);
Cit = (TextView)findViewById(R.id.txtCity);
PostCode = (TextView)findViewById(R.id.txtPost);
Coun = (TextView)findViewById(R.id.txtCountry);
Phon = (TextView)findViewById(R.id.txtPhone);
dob = (TextView)findViewById(R.id.txtDOB);
gender = (TextView)findViewById(R.id.txtGender);
yourDetails = (Button)findViewById(R.id.Button01);
payment = (Button)findViewById(R.id.Button02);
billing = (Button)findViewById(R.id.Button03);
history = (Button)findViewById(R.id.Button04);
yourDetails.setTypeface(arialFont); <-- Null Pointer exception starts here
payment.setTypeface(arialFont);
billing.setTypeface(arialFont);
history.setTypeface(arialFont);
try {
parseData(j);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The error I recieve from the code is this:
02-27 13:22:47.278: E/AndroidRuntime(929): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Ticketline.Ticketline/com.Ticketline.Ticketline.UserProfile}: java.lang.NullPointerException
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1796)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
02-27 13:22:47.278: E/AndroidRuntime(929): at com.Ticketline.Ticketline.TabGroupActivity.startChildActivity(TabGroupActivity.java:61)
02-27 13:22:47.278: E/AndroidRuntime(929): at com.Ticketline.Ticketline.Account.onCreate(Account.java:33)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.Activity.performCreate(Activity.java:4465)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-27 13:22:47.278: E/AndroidRuntime(929): ... 18 more
02-27 13:22:47.278: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException
02-27 13:22:47.278: E/AndroidRuntime(929): at com.Ticketline.Ticketline.UserProfile.onCreate(UserProfile.java:83)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.Activity.performCreate(Activity.java:4465)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-27 13:22:47.278: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-27 13:22:47.278: E/AndroidRuntime(929): ... 26 more
I know the issue is with setting the buttons typeface to Arial, the problem is I don't understand why this is an issue. If I comment the setTypeface lines out it works fine and will move onto other activity which do have their button typefaces set within the TabGroupActivity which perplexes me even more.
Top marks for anyone that can explain this to me
Figured the issue out, the coder who worked on this app before me gave the history button the id "btnHistory" instead of "Button04". Naturally this resolved in a null pointer error because the id "Button04" doesn't exist in that activity.

java.lang.ClassCastException: org.ksoap2.serialization.SoapObject in android

Hi i have developed one android application.
The app purpose is retrieve data from mysql database and display in android device.
This is my android code:
public class RetailerActivity extends Activity {
private final String NAMESPACE = "http://ws.testprops.com";
private final String URL = "http://krish.jelastic.servint.net/Retrieve/services/Fetch?wsdl";
private final String SOAP_ACTION = "http://ws.testprops.com/customerData";
private final String METHOD_NAME = "customerData";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
try {
ht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
SoapPrimitive s = response;
String str = s.toString();
String resultArr[] = str.split("&");//Result string will split & store in an array
TextView tv = new TextView(this);
for(int i = 0; i<resultArr.length;i++){
tv.append(resultArr[i]+"\n\n");
}
setContentView(tv);
} catch (Exception e) {
e.printStackTrace();
}
}}
If i have to run the app means blank screen only displayed.it is taking too long time nearly 1 hour
also am getting following error on my android console window:
11-05 10:38:27.868: W/System.err(837): java.lang.ClassCastException: org.ksoap2.serialization.SoapObject
11-05 10:38:27.878: W/System.err(837): at com.retailer.client.RetailerActivity.onCreate(RetailerActivity.java:29)
11-05 10:38:27.878: W/System.err(837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-05 10:38:27.878: W/System.err(837): at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 10:38:27.878: W/System.err(837): at android.os.Looper.loop(Looper.java:123)
11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-05 10:38:27.878: W/System.err(837): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 10:38:27.878: W/System.err(837): at java.lang.reflect.Method.invoke(Method.java:521)
11-05 10:38:27.878: W/System.err(837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-05 10:38:27.878: W/System.err(837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-05 10:38:27.888: W/System.err(837): at dalvik.system.NativeStart.main(Native Method)
please help me.how can i resolve this error.
Change
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
to
SoapObject response = (SoapObject)envelope.getResponse();

Categories