2017-04-22 41 views
-1

我在我的XML中有一個按鈕,我正在檢索活動的Java代碼的onCreate方法。但是,當我去運行應用程序時,會出現空指針異常。Android按鈕未被檢測到,並在嘗試檢索時返回null onCreate

XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.android.chargerpoints.LogonActivity"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/email" 
     android:hint="@string/prompt_email" 
     android:inputType="textEmailAddress" 
     android:maxLines="1" 
     android:singleLine="true" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/password" 
     android:hint="@string/prompt_password" 
     android:imeActionId="@+id/login" 
     android:imeActionLabel="@string/action_sign_in_short" 
     android:imeOptions="actionUnspecified" 
     android:inputType="textPassword" 
     android:maxLines="1" 
     android:singleLine="true"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/sign_in_button" 
     style="?android:textAppearanceSmall" 
     android:layout_marginTop="16dp" 
     android:text="@string/action_sign_in" 
     android:textStyle="bold"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/register_button" 
     style="?android:textAppearanceSmall" 
     android:layout_marginTop="8dp" 
     android:text="@string/action_register" 
     android:textStyle="bold"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/forgot_password_button" 
     style="?android:textAppearanceSmall" 
     android:layout_marginTop="16dp" 
     android:text="Forgot Password" 
     android:textStyle="bold"/> 

</LinearLayout> 

有問題的按鈕是一個id爲 「SIGN_IN_BUTTON」。這是java代碼中的onCreate方法: private TextView emailTextView; private TextView passwordTextView;

private Button signInButton; 
private Button registerButton; 
private Button forgotPasswordButton; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    realm = Realm.getDefaultInstance(); 

    emailTextView = (TextView) findViewById(R.id.email); 
    passwordTextView = (TextView) findViewById(R.id.password); 

    signInButton = (Button) findViewById(R.id.sign_in_button); 
    signInButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(LogonActivity.this, signIn(), 
        Toast.LENGTH_LONG).show(); 
     } 
    }); 

    registerButton = (Button) findViewById(R.id.register_button); 
    registerButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(LogonActivity.this, register(), 
        Toast.LENGTH_LONG).show(); 
     } 
    }); 

    forgotPasswordButton = (Button) findViewById(R.id.forgot_password_button); 
} 

這是我得到的錯誤:

E/AndroidRuntime: FATAL EXCEPTION: main 
              Process: com.example.android.chargerpoints, PID: 7276 
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.chargerpoints/com.example.android.chargerpoints.LogonActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
               at android.app.ActivityThread.-wrap12(ActivityThread.java) 
               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
               at android.os.Handler.dispatchMessage(Handler.java:102) 
               at android.os.Looper.loop(Looper.java:154) 
               at android.app.ActivityThread.main(ActivityThread.java:6077) 
               at java.lang.reflect.Method.invoke(Native Method) 
               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
               at com.example.android.chargerpoints.LogonActivity.onCreate(LogonActivity.java:42) 
               at android.app.Activity.performCreate(Activity.java:6662) 
               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
               at android.app.ActivityThread.-wrap12(ActivityThread.java)  
               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
               at android.os.Handler.dispatchMessage(Handler.java:102)  
               at android.os.Looper.loop(Looper.java:154)  
               at android.app.ActivityThread.main(ActivityThread.java:6077)  
               at java.lang.reflect.Method.invoke(Native Method)  
               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  

這將是驚人的,如果有人可以幫助我。我是這個東西的新手,這個問題讓我陷入困境,可能只是我錯過的一件小事。謝謝!

回答

2

您需要首先在活動/片段內擴充佈局。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.<xml file name>); 

    .... 
+0

太謝謝你了!它正在工作:) – svyas33

0
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
//without this line your activity class doesn't know which xml file you wanna use 
    setContentView(R.layout.yourlayoutname); 

    //and only after that you can retrive names 
    bnt = (Button) findViewById(R.id.btn)' 
    } 
+0

謝謝你的幫助! – svyas33