0

I'm beginner in android, OnClickListener generates exception, can any one help me out, if i have code commented then I dont see any exception ?

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v7.app.ActionBarActivity;

import android.view.*;

import android.widget.*;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        /*
        Button calc = (Button) findViewById(R.id.calcButton);
        calc.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v)
            { 
                EditText leftnum = (EditText) findViewById(R.id.leftnum);
                EditText rightnum = (EditText) findViewById(R.id.rightnum);
                //EditText func = (EditText) findViewById(R.id.func);
                TextView result = (TextView) findViewById(R.id.result);

                double leftnumdouble = Double.parseDouble(leftnum.getText().toString());
                double rightnumdouble = Double.parseDouble(rightnum.getText().toString());
                double resultnumdouble = leftnumdouble+rightnumdouble;
                result.setText(resultnumdouble + "");

            } 
        });
      */
    }

    @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;
    }

    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

Here's the layout I'm using:

<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="com.prkh.calculator.MainActivity$PlaceholderFragment" >

    <EditText
        android:id="@+id/leftnum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:ems="5"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/func"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/leftnum"
        android:layout_below="@+id/leftnum"
        android:layout_marginTop="30dp"
        android:ems="1"
        android:inputType="text"
        android:maxLength="1" />

    <EditText
        android:id="@+id/rightnum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/func"
        android:layout_below="@+id/func"
        android:layout_marginTop="30dp"
        android:ems="5"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/calcButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/rightnum"
        android:layout_below="@+id/rightnum"
        android:layout_marginTop="30dp"
        android:text="Calculate" />    

   <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/calcButton"
        android:layout_below="@+id/calcButton"
        android:layout_marginTop="60dp"
        android:ems="10">
    </TextView>



</RelativeLayout>

And this is the logcat:

05-15 05:18:33.708: D/AndroidRuntime(1682): Shutting down VM
05-15 05:18:33.708: W/dalvikvm(1682): threadid=1: thread exiting with uncaught exception (group=0xb2accba8)
05-15 05:18:33.748: E/AndroidRuntime(1682): FATAL EXCEPTION: main
05-15 05:18:33.748: E/AndroidRuntime(1682): Process: com.prkh.calculator, PID: 1682
05-15 05:18:33.748: E/AndroidRuntime(1682): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prkh.calculator/com.prkh.calculator.MainActivity}: 

java.lang.NullPointerException
05-15 05:18:33.748: E/AndroidRuntime(1682):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at android.os.Looper.loop(Looper.java:136)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at java.lang.reflect.Method.invoke(Method.java:515)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at dalvik.system.NativeStart.main(Native Method)
05-15 05:18:33.748: E/AndroidRuntime(1682): Caused by: java.lang.NullPointerException
05-15 05:18:33.748: E/AndroidRuntime(1682):     at com.prkh.calculator.MainActivity.onCreate(MainActivity.java:24)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at android.app.Activity.performCreate(Activity.java:5231)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 05:18:33.748: E/AndroidRuntime(1682):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-15 05:18:33.748: E/AndroidRuntime(1682):     ... 11 more
05-15 05:18:38.548: I/Process(1682): Sending signal. PID: 1682 SIG: 9
05-15 05:20:17.218: D/(1731): HostConnection::get() New Host Connection established 0xb8b90680, tid 1731
05-15 05:20:17.378: W/EGL_emulation(1731): eglSurfaceAttrib not implemented
05-15 05:20:17.398: D/OpenGLRenderer(1731): Enabling debug mode 0
05-15 05:20:24.608: I/Choreographer(1731): Skipped 31 frames!  The application may be doing too much work on its main thread.
05-15 05:20:27.458: W/IInputConnectionWrapper(1731): showStatusIcon on inactive InputConnection
Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
user3640030
  • 137
  • 1
  • 8
  • `Caused by: java.lang.NullPointerException at com.prkh.calculator.MainActivity.onCreate(MainActivity.java:24)` – Mikel May 15 '14 at 10:06
  • It could be that the ID for `calcButton` is out of date, which will make it return a `null` object. Try cleaning the project. – Overv May 15 '14 at 10:07

0 Answers0