Textview.settext()给出nullpointer异常

时间:2014-02-19 16:47:14

标签: android eclipse nullpointerexception

这是调用textview并获取意图的活动

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中获得空指针异常?

4 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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

答案 2 :(得分:0)

您应该始终在此环境中设置内容视图,然后在示例中使用任何小部件作为Textview

@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);



    }


}

这应该做的诀窍:) 希望它有所帮助

答案 3 :(得分:0)

我知道这已经过时但我会回答它,因为这段代码仍然适用。

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" />