0

I'm beginner in android development. I get this error message both in emulator and my real Android device. Please help me !!

public class MainActivity extends ActionBarActivity {
     Button btn1;
     TextView tv1;
     EditText edt1;
     String msj= "d";       

double n = Double.valueOf(edt1 . getText().toString());
double a = (1 / (Math.sqrt(5))); 
double b = ((1+(Math.sqrt(5)))/2); 
double c = ((1-(Math.sqrt(5)))/2); 
double i = a * ((Math.pow(b,n)) - (Math.pow(c,n)));
String k = String.valueOf(i);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   

   setContentView(R.layout.activity_main);
    btn1 = (Button) findViewById(R.id.btn1);
    final TextView tv1 = (TextView) findViewById(R.id.tv1);
    final EditText edt1 = (EditText) findViewById(R.id.edt1);

    btn1.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {               
            msj = edt1.getText().toString();
            tv1.setText( k );               
        }
    });

}

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

}

XML Code;

<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.example.fibonacci.MainActivity" >

<EditText
    android:id="@+id/edt1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:hint="@string/girdi"
    android:inputType="number" >

</EditText>

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/edt1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:text="@string/buton" />

<TextView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignLeft="@+id/edt1"
    android:layout_below="@+id/btn1"
    android:layout_marginTop="37dp"
    android:text="@string/cikti" />

</RelativeLayout>

I could't able to post the Log cat. Because of character limitation. HEre is the LogCat:

    0-13 17:10:48.907: I/Choreographer(380): Skipped 30 frames!  The application may be doing too much work on its main thread.
10-13 17:10:48.927: E/AndroidRuntime(1021): FATAL EXCEPTION: main
10-13 17:10:48.927: E/AndroidRuntime(1021): Process: com.example.fibonacci, PID: 1021
10-13 17:10:48.927: E/AndroidRuntime(1021): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.fibonacci/com.example.fibonacci.MainActivity}: java.lang.NullPointerException
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.os.Looper.loop(Looper.java:136)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at java.lang.reflect.Method.invoke(Method.java:515)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at dalvik.system.NativeStart.main(Native Method)
10-13 17:10:48.927: E/AndroidRuntime(1021): Caused by: java.lang.NullPointerException
10-13 17:10:48.927: E/AndroidRuntime(1021):     at com.example.fibonacci.MainActivity.<init>(MainActivity.java:20)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at java.lang.Class.newInstanceImpl(Native Method)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at java.lang.Class.newInstance(Class.java:1208)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-13 17:10:48.927: E/AndroidRuntime(1021):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
10-13 17:10:48.927: E/AndroidRuntime(1021):     ... 11 more
10-13 17:10:48.977: I/Choreographer(380): Skipped 47 frames!  The application may be doing too much work on its main thread.
10-13 17:10:48.977: W/ActivityManager(380):   Force finishing activity com.example.fibonacci/.MainActivity
10-13 17:10:49.387: I/WindowManager(380): Screenshot max retries 4 of Token{b2f8e630 ActivityRecord{b2f8e4d0 u0 com.example.fibonacci/.MainActivity t3 f}} appWin=Window{b30e4a38 u0 Starting com.example.fibonacci} drawState=4
10-13 17:10:49.387: W/WindowManager(380): Screenshot failure taking screenshot for (320x480) to layer 21010

Thanks for your help.

Dici
  • 25,226
  • 7
  • 41
  • 82
olcayakgn
  • 73
  • 1
  • 4

3 Answers3

0

You have an error at this line:

double n = Double.valueOf(edt1 . getText().toString());

In your class, n is a class level variable which gets initialized when the class is loaded. It is initialized before edt1 is initialized. edt1 is null at this point which basically throws you the error. There's way too much log information in the link you provided and I am not sure of there are other errors but this needs to be fixed for sure.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
0

As @Sunil made a point, this is probably gonna get you closer to your code to work. You have to calculate in your onClick method.

public class MainActivity extends ActionBarActivity {
    Button btn1;
    TextView tv1;
    EditText edt1;
    String msj= "d";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);
        tv1 = (TextView) findViewById(R.id.tv1);
        edt1 = (EditText) findViewById(R.id.edt1);
        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                double n = Double.valueOf(edt1.getText().toString());
                double a = (1 / (Math.sqrt(5))); 
                double b = ((1+(Math.sqrt(5)))/2); 
                double c = ((1-(Math.sqrt(5)))/2); 
                double i = a * ((Math.pow(b,n)) - (Math.pow(c,n)));
                String k = String.valueOf(i);
                msj = edt1.getText().toString();
                tv1.setText( k );               
            }
        });

    }

}
Marija Milosevic
  • 516
  • 4
  • 16
-1

I changed the code but the result is same :(

package com.example.fibonacci;

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 MainActivity extends ActionBarActivity {
    Button btn1;
    TextView tv1;
    EditText edt1;
    String msj= "d";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);
        tv1 = (TextView) findViewById(R.id.tv1);
        edt1 = (EditText) findViewById(R.id.edt1);

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                double n = Double.valueOf(edt1.getText().toString());
                double a = (1 / (Math.sqrt(5))); 
                double b = ((1+(Math.sqrt(5)))/2); 
                double c = ((1-(Math.sqrt(5)))/2); 
                double i = a * ((Math.pow(b,n)) - (Math.pow(c,n)));
                String k = String.valueOf(i);
                msj = edt1.getText().toString();
                tv1.setText( k );                           
            }
        });
    }

    @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);
    }
}
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
olcayakgn
  • 73
  • 1
  • 4
  • You marked my answer as correct, so did you manage to fix this? If not, please attach your logcat. Also, logcat that you attached in your question above is too big because its the whole systems logcat. You dont want this - you want to see only the logs your app created, so i advise you to *filter* your logcat log to get only logs coming from your app. http://stackoverflow.com/questions/6854127/filter-logcat-to-get-only-the-messages-from-my-application-in-android – Marija Milosevic Oct 15 '14 at 10:16