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

时间:2014-10-13 21:28:12

标签: java android eclipse logcat avd

我是android开发的初学者。我在模拟器和我的真实Android设备中都收到此错误消息。请帮帮我!!

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代码;

<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>

我无法发布Log cat。由于性格限制。 HEre是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

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

此行有错误:

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

在你的类中,n是一个类级变量,它在加载类时被初始化。它在初始化edt1之前初始化。此时edt1为null,它基本上会抛出错误。您提供的链接中的日志信息太多,我不确定是否存在其他错误,但这需要确定。

答案 1 :(得分:0)

正如@Sunil指出的那样,这可能会让你更接近你的代码来工作。你必须在你的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 );               
            }
        });

    }

}

答案 2 :(得分:-1)

我更改了代码但结果相同:(

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