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

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

问题描述:

这是我为我的应用程序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> 
+0

发表你的'LoginActivity'的代码。 –

+0

回答这个问题,很高兴看到活动代码'Login'和xml与布局 –

+2

您应该了解编译器错误(我的代码中没有错误 - 如果这是真的,您的应用程序不会崩溃! )。在修复编译器错误之前,您不能获得运行时错误,因为直到您执行为止,您无法运行应用程序。引发: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):在com.pmss.Login。 (Login.java:14)'我们需要看到Login类并显示第14行。 – Simon

你需要后检索您的元素充气布局,否则findViewById会返回null,因此您在login.setOnClickListener(/**/)处获得NPE

onCreate(Bundle)是您初始化您的活动的地方。大多数 重要的是,在这里你会通常所说setContentView(int)与 布局资源定义你的用户界面,并使用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); 
    /*****/ 
} 
+0

这就是说,“login =(Button)findViewById(R.id.login);”不应该在setContentView之前声明? – Nick

+0

@Nick在尝试检索元素之前,您总是需要膨胀包含元素的布局。 –

你的问题是在你的onCreate:

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

“登录” 将为空在这里,因为findViewById()确实需要已加载的布局,否则将无法找到该元素,它将为空。

设置监听器之前添加这一权利:

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

okok我想我明白你的意思......谢谢你的帮助 – Nick