有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法实例化activity ComponentInfo以启动我的应用程序

我在启动应用程序时遇到此错误,如何解决此问题

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

这是我的。xml文件:

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

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

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

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

</manifest>

这里是我的活动:

package com.javacodegeeks.安卓qrcodeexample;

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

import 安卓.app.Activity;
import 安卓.app.AlertDialog;
import 安卓.content.ActivityNotFoundException;
import 安卓.content.DialogInterface;
import 安卓.content.Intent;
import 安卓.net.Uri;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.EditText;
import 安卓.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
        }
    }

}

共 (1) 个答案

  1. # 1 楼答案

    移动行:

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