2016-07-06 72 views
-4

我越來越Android Studio中下面的錯誤,我無法來解決它:NPE錯誤Android Studio中

07-06 19:44:41.798 4491-4491/com.example.first_app D/AndroidRuntime: Shutting down VM 
07-06 19:44:41.799 4491-4491/com.example.first_app E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.first_app, PID: 4491 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first_app/com.example.first_app.welcome}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.view.View.toString()' on a null object reference 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.view.View.toString()' on a null object reference 
at com.example.first_app.welcome.onCreate(welcome.java:22) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
at android.app.ActivityThread.-wrap11(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5417)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
07-06 19:44:44.696 4491-4497/com.example.first_app W/art: Suspending all threads took: 46.612ms 
07-06 19:44:46.517 4491-4491/com.example.first_app I/Process: Sending signal. PID: 4491 SIG: 9 

這是錯誤IM招致


CODE FOR welcome.java

public class welcome extends AppCompatActivity { 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welcome); 
     TextView my_msg; 
     String name; 
     my_msg= (TextView)findViewById(R.id.textView3); 
     name= "Welcome " +findViewById(R.id.textView2).toString(); 
     my_msg.setText(name); 
    } 
} 

XML代碼FOR WEL come.xml ...請參閱it..it只顯示..名字是由用戶通過activity_form.java

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="1" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/textView3" 
     android:layout_gravity="center" 
     android:layout_weight="1" /> 

</LinearLayout> 
+1

'顯示java.lang.NullPointerException:嘗試調用虛擬方法 'java.lang.String中android.view.View.toString()'在com.example.first_app.welcome.onCreate的空對象引用上(welcome.java:22)' – CommonsWare

+0

看看'com.example.first_app.welcome'的第22行# – tyczj

+0

先生請詳細說明..怎麼做..其實m初學者 – prerika

回答

0

剛進屏幕上的「歡迎名稱」的消息,以解決您的例外type cast方式從該視圖

(TextView) findViewById(R.id.textView2).getText().toString(); 

查看和getText所以最終的字符串像

name= "Welcome " + (TextView) findViewById(R.id.textView2).getText().toString(); 

SUGGESTION

使用適當的命名約定view S,而不是命名view1view2