简体   繁体   English

Android应用程序已停止工作-Eclipse

[英]Android application has stopped working- Eclipse

I'm beginner in android development. 我是android开发的新手。 I get this error message both in emulator and my real Android device. 我在模拟器和真实的Android设备中都收到此错误消息。 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; XML代码;

<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. 我无法发布Log cat。 Because of character limitation. 由于字符限制。 HEre is the LogCat: 这里是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. 谢谢你的帮助。

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. 在您的类中,n是一个类级别变量,在加载该类时会对其进行初始化。 It is initialized before edt1 is initialized. 它在edt1初始化之前被初始化。 edt1 is null at this point which basically throws you the error. edt1此时为null,这基本上会引发您错误。 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. 您提供的链接中日志信息过多,我不确定是否还有其他错误,但是请务必对此进行修复。

As @Sunil made a point, this is probably gonna get you closer to your code to work. 正如@Sunil所指出的那样,这可能会使您更接近代码工作。 You have to calculate in your onClick method. 您必须使用onClick方法进行计算。

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

    }

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM