• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Getting error on onStop()

Here my code...

Java:
public class MainActivity extends ActionBarActivity {

    int counter = 0;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


       Typeface type = Typeface.createFromAsset(getAssets(),"fonts/256BYTES.TTF");
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setTypeface(type);

        tv = (TextView) findViewById(R.id.textView1);
        SharedPreferences setting = getSharedPreferences("Counter",0);
        tv.setText(setting.getString("tvalue","000"));

    }


    public void ClickButton(View view)
    {
        counter++;
        TextView tv1 = (TextView) findViewById(R.id.textView1);
        tv1.setText(String.valueOf(counter));
    }


    public  void ResetButton(View view)
    {

        counter = 0;
        TextView tv1 = (TextView) findViewById(R.id.textView1);
        tv1.setText("00");
    }

    @Override
    protected void onStop() {
        super.onStop();

        SharedPreferences setting = getSharedPreferences("Counter",0);
        SharedPreferences.Editor editor = setting.edit();
        editor.putString("tvalue",tv.getText().toString());
        editor.commit();
    }

}
 
Welcome to Android forums then.:) No worries about the thread move, I'm just trying to get the best eyes on it. Good luck with the app.
 
Welcome to our AndroidForums, Haider! :)

Could you also share what the error you're getting is (stack trace, etc.)?

That will certainly help those reading with how they might help you.

Cheers!
 
Welcome to our AndroidForums, Haider! :)

Could you also share what the error you're getting is (stack trace, etc.)?

That will certainly help those reading with how they might help you.

Cheers!
here....

06-28 23:58:36.411 624-624/com.example.haider885.counter E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to stop activity {com.example.haider885.counter/com.example.haider885.counter.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2624)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2690)
at android.app.ActivityThread.access$2100(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:964)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
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:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.haider885.counter.MainActivity.onStop(MainActivity.java:58)
at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1170)
at android.app.Activity.performStop(Activity.java:3873)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2690)
at android.app.ActivityThread.access$2100(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:964)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
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:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
 
Thanks!

Can you point-out exactly which statement is line #58 in your program?

Your posted code is helpful, but it has a new/different set of line numbers than what I'm sure is in your Eclipse or Android Studio IDE editor.

You're getting a NullPointerException error:

Caused by: java.lang.NullPointerException
at com.example.haider885.counter.MainActivity.onStop(MainActivity.java:58)

and I'm guessing you've got an uninitialized variable, etc.

If this were me, I'd add a Log.v statement right before the error you're getting an exception on to display the value of the variables involved in that statement--that should help you narrow-down what the issue is.

Cheers!
 
Thanks!

Can you point-out exactly which statement is line #58 in your program?

Your posted code is helpful, but it has a new/different set of line numbers than what I'm sure is in your Eclipse or Android Studio IDE editor.

You're getting a NullPointerException error:



and I'm guessing you've got an uninitialized variable, etc.

If this were me, I'd add a Log.v statement right before the error you're getting an exception on to display the value of the variables involved in that statement--that should help you narrow-down what the issue is.

Cheers!
here is Line 58...

editor.putString("tvalue",tv.getText().toString());
 
Last edited:
Ah, I think the issue is that you are re-declaring/defining TextView tv locally when you should just be referencing the global TextView tv delcaration (declared at the top of your program).

Remove the "TextView" portions from "Textview tv = ...." lines in both your onCreate and the ClickButton and ResetButton methods so that they reference the "global" TextView tv declaration.

Make sense?
 
Ah, I think the issue is that you are re-declaring/defining TextView tv locally when you should just be referencing the global TextView tv delcaration (declared at the top of your program).

Remove the "TextView" portions from "Textview tv = ...." lines in both your onCreate and the ClickButton and ResetButton methods so that they reference the "global" TextView tv declaration.

Make sense?
Nope, I don't get it...
Actually I am new in development.....Can you modify code for me???
 
Back
Top Bottom