-1

Got an error when i tried to run my program... no clue whats going, please help im really new to java. Its a simple gui of a calculator and im trying to have text appear in a textview when a button is pressed.

    package com.ab.calculator;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Calc1 extends ActionBarActivity {
    Button btn7;
    TextView txt = (TextView)findViewById(R.id.textView);
    int x,y,z;

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


        // btn7.setOnClickListener(new OnClickListener() {

    }
    void updatescore(){
        TextView text = (TextView) findViewById(R.id.textView);
        text.setText(x );
    }

    public void onToggleClicked(View view) {
        btn7 = (Button)findViewById(R.id.button2);
        boolean on = ((Button) view).performClick();

        if (on) {
            x = 7;
        }
        updatescore();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_calc1, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Heres the LogCat

    10-22 17:21:47.160 13640-13640/? D/dalvikvm: Late-enabling CheckJNI
    10-22 17:21:47.261 13640-13640/com.ab.calculator D/AndroidRuntime: Shutting down VM
    10-22 17:21:47.261 13640-13640/com.ab.calculator W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x416cc930)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime: FATAL EXCEPTION: main
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ab.calculator/com.ab.calculator.Calc1}: java.lang.NullPointerException
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:  Caused by: java.lang.NullPointerException
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.Activity.findViewById(Activity.java:1839)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at com.ab.calculator.Calc1.<init>(Calc1.java:16)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at java.lang.Class.newInstanceImpl(Native Method)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at java.lang.Class.newInstance(Class.java:1319)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041) 
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    10-22 17:21:47.269 13640-13640/com.ab.calculator E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
    10-22 17:22:37.457 13640-13640/? I/Process: Sending signal. PID: 13640 SIG: 9

1 Answers1

2

You have to move your TextView assignment to the onCreate() method. So Android knows which layout it should inflate after setting setContentView(...).

protected void onCreate(Bundle savedInstanceState){

    super(...);
    setContentView(...);
    txt = (TextView)findViewById(R.id.textView);

}

Edit: I have seen that you fetch the TextView everytime you need it by findViewById(...). It is even more powerful if you only assign it in onCreate and then use this reference to the TextView.

Lunero
  • 666
  • 1
  • 8
  • 17