2016-05-01 18 views
0

nicht lösen Ich bin ein Android-Anfänger und ich baue eine Test-App, um zu wissen, was ich tun kann und was ich nicht kann, und ich versuche, diesen Laufzeitfehler zu lösen Ich weiß nicht, was ich machen soll. Kann mir bitte jemand helfen? Ich poste meinen Code und den FehlerKann Laufzeitfehler auf Android Studio 2.1

MainActvity.java:

package com.example.android.myapplication; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends AppCompatActivity { 

    EditText uname; 

    { 
     uname = (EditText) findViewById(R.id.username); 
    } 

    EditText pword; 

    { 
     pword = (EditText) findViewById(R.id.password); 
    } 

    Button btn; 

    { 
     btn = (Button) findViewById(R.id.button); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     OnClickButtonListener(); 
    } 
    public void OnClickButtonListener() { 
     btn.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         Intent intent = new Intent("com.example.android.myapplication.Activity2"); 
         startActivity(intent); 
        } 
       } 
     ); 
    } 
} 

Log:

05-01 14:32:21.725 32378-32378/com.example.android.myapplication E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.android.myapplication, PID: 32378 
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.myapplication/com.example.android.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2555) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2773) 
    at android.app.ActivityThread.access$900(ActivityThread.java:177) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1434) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5930) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
    at android.app.Activity.findViewById(Activity.java:2172) 
    at com.example.android.myapplication.MainActivity.<init>(MainActivity.java:12) 
    at java.lang.reflect.Constructor.newInstance(Native Method) 
    at java.lang.Class.newInstance(Class.java:1690) 
    at android.app.Instrumentation.newActivity(Instrumentation.java:1078) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2545) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2773)  
    at android.app.ActivityThread.access$900(ActivityThread.java:177)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1434)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:135)  
    at android.app.ActivityThread.main(ActivityThread.java:5930)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:372)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)  

Antwort

2

Verschieben Sie alle drei Ihrer Aussagen wie diese:

uname = (EditText) findViewById(R.id.username); 

in onCreate() , nach Ihrem setContentView() Anruf, aus zwei Gründen:

  1. In der Regel können Sie nicht sicher Methoden aufrufen, die Sie von Activity bis erben nach super.onCreate() aufgerufen wurde

  2. Sie speziell findViewById() nicht aufrufen können, bis Sie wirklich die Ansichten haben, wie durch setContentView()

  3. Aufruf
+0

Woow vielen Dank, die sehr geholfen haben !! –

1

Nehmen Sie diese drei Codezeilen in die oncreate-Methode nach dem Aufruf der setcontentview-Methode. Das initialisiert Ihre Ansicht und Ihre NULLPOINTEREXCEPTION wird nicht passieren.

uname = (EditText) findViewById(R.id.username); 
pword = (EditText) findViewById(R.id.password); 
btn = (Button) findViewById(R.id.button); 
Verwandte Themen