java - 无法实例化启动我的应用程序的 ComponentInfo Activity

标签 java android view android-view

启动应用程序时出现此错误,如何解决此问题?

12112-12112/com.javacodegeeks.androidqrcodeexample E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.javacodegeeks.androidqrcodeexample/com.javacodegeeks.androidqrcodeexample.Login}: java.lang.NullPointerException

这是我的 .xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.javacodegeeks.androidqrcodeexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.javacodegeeks.androidqrcodeexample.AndroidBarcodeQrExample"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.javacodegeeks.androidqrcodeexample.Login"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的 Activity :

package com.javacodegeeks.androidqrcodeexample;

/**
 * Created by Andre on 14/06/15.
 */

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class Login  extends Activity {

    EditText username = (EditText)findViewById(R.id.editText);
    EditText password = (EditText)findViewById(R.id.editText2);

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }

    public void login(View view) {
        if (username.getText().toString().equals("admin") && password.getText().toString().equals("admin")) {
            Toast toast = Toast.makeText(this, "CORRECT PW", Toast.LENGTH_LONG);
            toast.show();
            //correcct password
            Intent i = new Intent(getApplicationContext(), AndroidBarcodeQrExample.class);
            i.putExtra("Disco", username.getText().toString());
            startActivity(i);
        } else {
            Toast toast = Toast.makeText(this, "UNCORRECT PW", Toast.LENGTH_LONG);
            toast.show();
            //wrong password
        }
    }

}

最佳答案

移动线:

EditText username = (EditText)findViewById(R.id.editText);
EditText password = (EditText)findViewById(R.id.editText2);

内部onCreate

EditText username;
EditText password;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    username = (EditText)findViewById(R.id.editText);
    password = (EditText)findViewById(R.id.editText2);
}

关于java - 无法实例化启动我的应用程序的 ComponentInfo Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30834326/

相关文章:

java - 如何最好地打包一个网站

android - 在 Android 中的计时器内运行异步任务

java - 使用 apktool 更改版本号

java - 依赖注入(inject) : binding to specific value of a constructor parameter

ios - 为什么我们需要在成功解雇转换后从容器 View 中删除 toView?

Java - 场景生成器 - 切换场景后,文本字段和组合框中的用户输入消失

java - 使用pentaho java API为以编程方式传递hdfs文件路径的hdfs文件生成报告

java - 在 Eclipse 中将项目构建为可执行 jar 文件时,缓冲图像不呈现

asp.net-mvc-3 - 以 MVC3 形式并排显示两个文本框

templates - 你如何在 Go 中定义 View 模型