2

i am a beginner in the android, and i dont know what is the problem in my code ! and i need your help, this is my code: i am trying to do a calculator = - / * ..... and i removed the if view==pbtn for example...just to try the onclick function...and i cant find what is my problem

package com.michael.mikematta.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static com.michael.mikematta.calculator.R.id.FirstNum;
import static com.michael.mikematta.calculator.R.id.HelokBtn;
import static com.michael.mikematta.calculator.R.id.KafolBtn;
import static com.michael.mikematta.calculator.R.id.MinusBtn;
import static com.michael.mikematta.calculator.R.id.PlusBtn;
import static com.michael.mikematta.calculator.R.id.SecondNum;
import static com.michael.mikematta.calculator.R.id.TheResult;


public class MainActivity extends Activity implements View.OnClickListener {

    EditText firstnumx;
    EditText secondnum;
    Button pbtn;
    Button minusbtn;
    Button kafolbtn;
    Button helokbtn;
    TextView theresult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button pbtn = (Button) findViewById(R.id.PlusBtn);
        Button minusbtn = (Button) findViewById(R.id.MinusBtn);
        Button kafolbtn = (Button) findViewById(R.id.KafolBtn);
        Button helokbtn = (Button) findViewById(R.id.HelokBtn);
        EditText firstnum = (EditText) findViewById(R.id.FirstNum);
        EditText secondnum = (EditText) findViewById(R.id.SecondNum);
        TextView theresult = (TextView) findViewById(R.id.TheResult);

        minusbtn.setOnClickListener(this);
        pbtn.setOnClickListener(this);
        kafolbtn.setOnClickListener(this);
        helokbtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {


        theresult.setText("hi");


    }
}

my xml:

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/ic_launcher"
        android:orientation="vertical"

        tools:context=".MainActivity">

        <TextView
            android:text="Hello Calculator"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:textSize="20dp"
            android:textColor="#000"
            android:textStyle="bold"
            />
        <EditText
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:hint="Enter First Num"
            android:background="#fff"
            android:id="@+id/FirstNum"
            android:layout_margin="10dp"
            />
        <EditText
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:hint="Enter Second Num"
            android:background="#fff"
            android:id="@+id/SecondNum"
            android:layout_margin="10dp"
            />
        <Button
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="+"
            android:background="#ffeeeeee"
            android:textSize="22dp"
            android:id="@+id/PlusBtn"
            android:layout_margin="10dp"
            />
        <Button
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="-"
            android:background="#ffeeeeee"
            android:textSize="22dp"
            android:id="@+id/MinusBtn"
            android:layout_margin="10dp"
            />
        <Button
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="*"
            android:background="#ffeeeeee"
            android:textSize="22dp"
            android:id="@+id/KafolBtn"
            android:layout_margin="10dp"
            />
        <Button
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="/"
            android:background="#ffeeeeee"
            android:textSize="22dp"
            android:id="@+id/HelokBtn"
            android:layout_margin="10dp"
            />
        <TextView
            android:text="Here will be your result"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:textSize="20dp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:background="#ffff714f"
            android:id="@+id/TheResult"
            />

    </LinearLayout>

Exception:

02-08 03:09:50.468    1648-1666/com.michael.mikematta.calcu2 W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-08 03:09:50.468    1648-1666/com.michael.mikematta.calcu2 W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaeeff540, error=EGL_SUCCESS
02-08 03:09:50.538    1648-1648/com.michael.mikematta.calcu2 I/Choreographer﹕ Skipped 33 frames!  The application may be doing too much work on its main thread.
02-08 03:09:51.177    1648-1648/com.michael.mikematta.calcu2 I/Choreographer﹕ Skipped 37 frames!  The application may be doing too much work on its main thread.
02-08 03:09:53.984    1648-1648/com.michael.mikematta.calcu2 D/AndroidRuntime﹕ Shutting down VM
02-08 03:09:54.000    1648-1648/com.michael.mikematta.calcu2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.michael.mikematta.calcu2, PID: 1648
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
            at com.michael.mikematta.calcu2.MainActivity.onClick(MainActivity.java:47)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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)
02-08 03:09:57.267    1648-1648/com.michael.mikematta.calcu2 I/Process﹕ Sending signal. PID: 1648 SIG: 9

any help please?

11
  • What is the problem... ? Explain it properly. Commented Feb 8, 2015 at 0:46
  • onclick the app crash @SarveshKumarSingh Commented Feb 8, 2015 at 0:49
  • where is the Error log from LogCat... ? Commented Feb 8, 2015 at 0:57
  • @MikhaMatta : First, remove all of the lines to import static... for the R.id entries. Who told you to do that? Second, post your activity_main.xml file and also your Logcat stacktrace showing the crash. Commented Feb 8, 2015 at 0:57
  • Well I think I got the error. Commented Feb 8, 2015 at 1:00

2 Answers 2

5

You are initializing a local copy of variable theresult in onCreate:

TextView theresult = (TextView) findViewById(R.id.TheResult);

But in onClick you are trying to access another instance of the variable theresult which is declared outside onCreate. This one has not been initialized yet. That's why giving its giving null when you are trying to do setText.

To solve, replace the below line:

TextView theresult = (TextView) findViewById(R.id.TheResult);

with

theresult = (TextView) findViewById(R.id.TheResult);
Sign up to request clarification or add additional context in comments.

1 Comment

@MikhaMatta Try the solution in answer and let me know if it works.
0

Either check you have initialized the view or may be layout not has been attached to activity.

    password = (EditText) findViewById(R.id.password);

or

setContentView(R.layour.login_screen);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.