不幸的是app已停止错误

时间:2013-11-13 17:44:48

标签: android

之前尝试过一次这个错误,发现xml文件中的onclick属性是问题,我修复了它并且它有效。

现在我得到了同样的错误,我无法弄清楚如何将错误消息从logcat转换为有用的任何内容可以帮助吗?

代码是:

package dk.SimonPedersen.gem;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

public static final String GEMFFS = "dk.SimonPedersen.GEMFFS";



SharedPreferences ada;
SharedPreferences.Editor editor;
Button btnGem;
ToggleButton tbKon;
EditText etVaegt;
Double sex, vaegt, KT;
String lols;
TextView haha = (TextView) findViewById(R.id.haha); 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ada = getSharedPreferences(GEMFFS, 0);
    editor = ada.edit();


    btnGem = (Button) findViewById (R.id.btnGem);
    tbKon = (ToggleButton) findViewById (R.id.tbKon);
    etVaegt = (EditText) findViewById (R.id.etVaegt);



    tbKon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                sex=0.7d;
            } else {
                sex=0.6d;
            }
           Log.d("Konskonstant", "sex" + sex);
        }
    });



    btnGem.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub



            lols=etVaegt.getText().toString();
            vaegt=Double.parseDouble(lols);
            KT = sex*vaegt;

            haha.setText("KT " + KT);
        }

}); 
}

}

Logcat是:

11-13 18:33:04.870: D/AndroidRuntime(30475): Shutting down VM
11-13 18:33:04.870: W/dalvikvm(30475): threadid=1: thread exiting with uncaught exception (group=0x414e4700)
11-13 18:33:04.870: E/AndroidRuntime(30475): FATAL EXCEPTION: main
11-13 18:33:04.870: E/AndroidRuntime(30475): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{dk.SimonPedersen.gem/dk.SimonPedersen.gem.MainActivity}: java.lang.NullPointerException
11-13 18:33:04.870: E/AndroidRuntime(30475):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at android.app.ActivityThread.access$600(ActivityThread.java:156)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at android.os.Looper.loop(Looper.java:137)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at android.app.ActivityThread.main(ActivityThread.java:5303)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at java.lang.reflect.Method.invokeNative(Native Method)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at java.lang.reflect.Method.invoke(Method.java:525)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at dalvik.system.NativeStart.main(Native Method)
11-13 18:33:04.870: E/AndroidRuntime(30475): Caused by: java.lang.NullPointerException
11-13 18:33:04.870: E/AndroidRuntime(30475):    at android.app.Activity.findViewById(Activity.java:1864)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at dk.SimonPedersen.gem.MainActivity.<init>(MainActivity.java:28)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at java.lang.Class.newInstanceImpl(Native Method)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at java.lang.Class.newInstance(Class.java:1130)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
11-13 18:33:04.870: E/AndroidRuntime(30475):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
11-13 18:33:04.870: E/AndroidRuntime(30475):    ... 11 more

1 个答案:

答案 0 :(得分:1)

您无法在onCreate

之外定义此内容
TextView haha = (TextView) findViewById(R.id.haha);

你可以做的是:

TextView haha; //here

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ada = getSharedPreferences(GEMFFS, 0);
editor = ada.edit();

haha = (TextView) findViewById(R.id.haha); // and here
btnGem = (Button) findViewById (R.id.btnGem);
tbKon = (ToggleButton) findViewById (R.id.tbKon);
etVaegt = (EditText) findViewById (R.id.etVaegt);

// continue with the rest of the code...