"不幸的是,App名称已停止"简单的应用

时间:2015-11-08 20:13:05

标签: android

我开始学习Android应用开发。在Android Studio中,gradle没有显示任何错误。但显示"不幸的是,App名称已停止"在我的手机上。我试图通过按钮应用程序将简单的EditTex制作为textview。我认为这很容易,但显然不是。至少有这个致命的错误。我希望你能在这种情况下帮助我。也许这是个问题。

这里是logcat

I/art: Late-enabling -Xcheck:jni
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.winwin.kapurs.tester1, PID: 26610
E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.winwin.kapurs.tester1/com.winwin.kapurs.tester1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5292)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
E/AndroidRuntime: at android.app.Activity.findViewById(Activity.java:2072)
E/AndroidRuntime: at com.winwin.kapurs.tester1.MainActivity.<init>(MainActivity.java:11)
E/AndroidRuntime: at java.lang.reflect.Constructor.newInstance(Native Method)
E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1606)
E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154) 
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135) 
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5292) 
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)

这是我的主要代码:

public class MainActivity extends AppCompatActivity {

    final EditText mess = (EditText) findViewById(R.id.editText);
    final TextView txt = (TextView) findViewById(R.id.textView);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        }

    public void onClick(View v) {
        if (txt.getText().toString().equals("")) {
            String editTextValue = mess.getText().toString();
            txt.setText(editTextValue);
        } else {
            txt.setText("");
            mess.setText("");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

移动这些初始化(保留变量声明)

final EditText mess = (EditText) findViewById(R.id.editText);
final TextView txt = (TextView) findViewById(R.id.textView);

onCreate(),因为活动窗口尚未初始化,并且在setContentView()之后,因为返回的值可能为非空,所以它赢得了NPE。

答案 1 :(得分:0)

你可以这样做:(变量不一定是git read-tree -um

final