我看到我的编码没有错误,但是当我在我的应用程序上运行它强制关闭

时间:2013-11-24 15:44:21

标签: java android eclipse forceclose

这是我的应用程序com.pmss

的logcat
11-24 23:31:04.799: D/AndroidRuntime(22791): Shutting down VM
11-24 23:31:04.799: W/dalvikvm(22791): threadid=1: thread exiting with uncaught exception (group=0x40018578)
11-24 23:31:04.809: E/AndroidRuntime(22791): FATAL EXCEPTION: main
11-24 23:31:04.809: E/AndroidRuntime(22791): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.pmss/com.pmss.Login}: java.lang.NullPointerException
11-24 23:31:04.809: E/AndroidRuntime(22791):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at android.os.Looper.loop(Looper.java:130)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at android.app.ActivityThread.main(ActivityThread.java:3687)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at java.lang.reflect.Method.invokeNative(Native Method)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at java.lang.reflect.Method.invoke(Method.java:507)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at dalvik.system.NativeStart.main(Native Method)
11-24 23:31:04.809: E/AndroidRuntime(22791): Caused by: java.lang.NullPointerException
11-24 23:31:04.809: E/AndroidRuntime(22791):    at android.app.Activity.findViewById(Activity.java:1647)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at com.pmss.Login.<init>(Login.java:14)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at java.lang.Class.newInstanceImpl(Native Method)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at java.lang.Class.newInstance(Class.java:1409)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-24 23:31:04.809: E/AndroidRuntime(22791):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
11-24 23:31:04.809: E/AndroidRuntime(22791):    ... 11 more

这是我的清单文件,如下所示

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
    <activity
        android:name="com.pmss.Login"
        android:label="@string/app_main" >
        <intent-filter android:label="@string/app_name" >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.pmss.Register"
        android:label="@string/button_register"
        android:parentActivityName="com.pmss.Login" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.pmss.Login" />
    </activity>
    <activity
        android:name="com.pmss.MainMenu"
        android:label="@string/mainmenu"
        android:parentActivityName="com.pmss.Login" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.pmss.Login" />
    </activity>
</application>

我不知道是什么问题,因为我的编码没有发生错误。

这是我的Login.java,如下所示

package com.pmss;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Login extends ActionBarActivity {

Button login = (Button) findViewById(R.id.login);
Button register = (Button) findViewById(R.id.register);

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);

    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Login.this, MainMenu.class);
            startActivity(intent);
        }
    });

    register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Login.this, Register.class);
            startActivity(intent);
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // For the main activity, make sure the app icon in the action bar
        // does not behave as a button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

}

这是我的Login.xml,如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
tools:ignore="ExtraText" >

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:ems="10"
    android:inputType="textPassword" />

<Button
    android:id="@+id/register"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/password"
    android:layout_marginBottom="26dp"
    android:gravity="center"
    android:text="@string/button_register" />

<TextView
    android:id="@+id/newuser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/register"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="14dp"
    android:text="@string/newuser" />

<EditText
    android:id="@+id/userid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignRight="@+id/password"
    android:layout_marginBottom="14dp"
    android:ems="10"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/userid"
    android:layout_centerHorizontal="true"
    android:text="@string/userid"
    android:textSize="20sp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/password"
    android:layout_centerHorizontal="true"
    android:text="@string/password"
    android:textSize="20sp" />

<Button
    android:id="@+id/login"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/password"
    android:layout_marginTop="14dp"
    android:gravity="center"
    android:text="@string/button_login" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

您需要在强化布局后检索元素,否则findViewById将返回null,因此您会在NPE获得login.setOnClickListener(/**/)

  

onCreate(Bundle)是您初始化活动的地方。最   重要的是,在这里你通常会打电话给setContentView(int)   布局资源定义您的UI,并使用findViewById(int)来   检索该UI中需要与之交互的小部件   编程。


Button login;
Button register;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login); //<-- here the layout is inflated
    //Now you can retrieve your elements from the XML file.
    login = (Button) findViewById(R.id.login);
    register = (Button) findViewById(R.id.register);
    /*****/
}

答案 1 :(得分:0)

你的问题出在你的onCreate:

login.setOnClickListener(new View.OnClickListener()...

这里的“login”将为null,因为findViewById()确实需要已经加载的布局,否则它将找不到元素并且它将为null。

在设置侦听器之前添加此权限:

login = (Button) findViewById(R.id.login);