2014-02-19 88 views
3

這是被調用的TextView並獲得意圖Textview.settext()給空指針異常

package com.example.smartbrowser; 

import android.os.Bundle; 

import android.app.Activity; 

import android.content.Intent; 

import android.view.Menu; 

import android.widget.TextView; 

public class MainActivity extends Activity { 


@Override 

protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

TextView vw =(TextView) findViewById(R.id.text); 

Intent intent =getIntent(); 

String message = intent.getStringExtra(Browseractivity.Message); 


vw.setText(message); 


setContentView(R.layout.activity_main); 
    } 


} 

而且activity_main.xml中

<RelativeLayout 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: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=".MainActivity" > 


<TextView 

    android:id="@+id/text" 

    android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:layout_alignParentLeft="true" 

    android:layout_alignParentTop="true" 


    /> 


</RelativeLayout> 

爲什麼我在MainActivity獲得空指針異常活動。 java嗎?

+0

具有u打印消息toast.maketextü必須通過空值在字符串第一o f所有打印他的消息在 Toast.MakeText(getApplicationContext(),「message」+ message,Toast.length_short).show();它會幫助你做到這一點 – Amitsharma

+0

你傳遞的字符串null的值實際上不是在字符串消息....所以你有這個問題...我想你找到他的錯誤..... – Amitsharma

回答

0

更改爲

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); // should be first 
TextView vw =(TextView) findViewById(R.id.text); // then initialize textview 
Intent intent =getIntent(); 
String message = intent.getStringExtra(Browseractivity.Message); 
vw.setText(message); 
} 

您需要首先設置佈局,活動的內容,然後初始化你的意見。

findViewById尋找當前充氣佈局中的視圖。在將佈局設置爲活動之前,您已初始化NullPointerException bcoz TextView

3

你必須在初始化任何視圖之前先放置setContentView(R.layout.activity_main);。這裏,在設置你的內容視圖之前初始化你的TextView,這就是爲什麼它返回null

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    TextView vw =(TextView) findViewById(R.id.text); 

    Intent intent =getIntent(); 

    String message = intent.getStringExtra(Browseractivity.Message); 
    vw.setText(message); 
} 
0

你應該總是在這個circonstance設置你的內容視圖在你的榜樣

@Override 

protected void onCreate(Bundle savedInstanceState) { 


super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

TextView vw =(TextView) findViewById(R.id.text); 

Intent intent =getIntent(); 

String message = intent.getStringExtra(Browseractivity.Message); 


vw.setText(message); 



    } 


} 

這應該做的伎倆:) 希望它幫助

0

與任何部件扮演一個TextView前我知道這是舊的,但我會回答它,因爲這些代碼仍然適用。

MainActivity.java

public class MainActivity extends Activity { 

private TextView textView2; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_main); // Change from activity_main > fragment_main 
    textView2 = (TextView)findViewById(R.id.textview2); 
    textView2.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View textView2) { 
      // TODO Auto-generated method stub 

      if (textView2.isClickable() == true){ 
       ((TextView) textView2).setText("Changed!!!!!!");      
      } 
     } 
    }); 


    if (savedInstanceState == null) { 
     getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit(); 
    } 
} 

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
<!-- REMOVE android:id="@+id/container" --> 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.thinktankcoding.androiddevtest.MainActivity" 
tools:ignore="MergeRootFrame" /> 

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/container" <!-- ADD THE LINE YOU REMOVED HERE AND REMOVE THESE COMMENTS --> 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
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.thinktankcoding.androiddevtest.MainActivity$PlaceholderFragment" > 

<TextView 
    android:id="@+id/textview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<TextView 
    android:id="@+id/textview2" 
    android:layout_width="fill_parent" 
    android:lines="2" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textview1" 
    android:layout_below="@+id/textview1" 
    android:clickable="true" /> 

+0

請避免只是**粘貼代碼**,一點解釋永遠不會受到傷害。 – eRaisedToX