简体   繁体   English

无法解决Android Studio 2.1上的运行时错误

[英]Can't solve Runtime error on Android Studio 2.1

I'm an Android beginner and I'building a test app to know what I can do and what I can't, and I'm trying to solve this Runtime error and don't know what to do. 我是一名Android初学者,我正在构建一个测试应用程序,以了解我可以做什么和不可以做什么,并且我正在尝试解决此运行时错误并且不知道该怎么做。 Can anyone help me please? 谁能帮我吗? I post my code and the error 我发布我的代码和错误

MainActvity.java: MainActvity.java:

package com.example.android.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText uname;

    {
        uname = (EditText) findViewById(R.id.username);
    }

    EditText pword;

    {
        pword = (EditText) findViewById(R.id.password);
    }

    Button btn;

    {
        btn = (Button) findViewById(R.id.button);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OnClickButtonListener();
    }
    public void OnClickButtonListener() {
        btn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("com.example.android.myapplication.Activity2");
                        startActivity(intent);
                    }
                }
        );
    }
}

Log: 日志:

05-01 14:32:21.725 32378-32378/com.example.android.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.myapplication, PID: 32378
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.myapplication/com.example.android.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2555)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2773)
    at android.app.ActivityThread.access$900(ActivityThread.java:177)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1434)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5930)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
    at android.app.Activity.findViewById(Activity.java:2172)
    at com.example.android.myapplication.MainActivity.<init>(MainActivity.java:12)
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at java.lang.Class.newInstance(Class.java:1690)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2545)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2773) 
    at android.app.ActivityThread.access$900(ActivityThread.java:177) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1434) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5930) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

Move all three of your statements like this one: 像这样移动所有三个语句:

uname = (EditText) findViewById(R.id.username);

into onCreate() , after your setContentView() call, for two reasons: setContentView()调用之后放入onCreate() ,原因有两个:

  1. In general, you cannot safely call methods that you inherit from Activity until after super.onCreate() has been called 通常,只有在调用super.onCreate()之后,才能安全地调用从Activity继承的方法

  2. You specifically cannot call findViewById() until you actually have the views, such as by calling setContentView() 具体来说,您只有在实际拥有视图之前才能调用findViewById() ,例如通过调用setContentView()

Take these three line of code to oncreate method after the setcontentview method recall. 调用setcontentview方法后,将这三行代码带入oncreate方法。 That initializes your view and your NULLPOINTEREXCEPTION will not happen. 这将初始化您的视图,并且不会发生NULLPOINTEREXCEPTION。

uname = (EditText) findViewById(R.id.username);
pword = (EditText) findViewById(R.id.password);
btn = (Button) findViewById(R.id.button);

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

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