到TextView的分配值时,空指针异常 [英] NullPointer Exception when assigning value to TextView

查看:199
本文介绍了到TextView的分配值时,空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想从一个活动的价值传递到另一个,但无法从该视图中找到TextView的价值..我只是路过一个消息从主要活动到本次活动通过一个文本框,我在这里看到的文本框的值,但不能赋值给TextView的...

I just wanted to passing value from one activity to another but not able to find Textview value from the view.. I just passing one message from main activity to this activity through a text box and I'm seeing textbox value here but not able to assign that to textView...

这是我的code ..

here is my code..

public class DisplayMessageActivity extends Activity {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

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

    textView = (TextView) findViewById(R.id.displayText);
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    textView.setText(message);


}

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="com.example.activityapps.DisplayMessageActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/displayText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="TextView" />

请帮我..

推荐答案

的TextView 不属于活动布局。移动的TextView 的初始化 onCreateView PlaceholderFragment

The TextView does not belong to the Activity layout. Move the initialization of TextView to onCreateView of PlaceholderFragment.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
    TextView textView = (TextView) rootView.findViewById(R.id.displayText);
    return rootView;
}

在活动

   if (savedInstanceState == null) {
   String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
   Bundle bundle = new Bundle();
   bundle.putString("key", message);
   PlaceholderFragment fragobj = new   PlaceholderFragment();
   fragobj.setArguments(bundle);
        getFragmentManager().beginTransaction()
                .add(R.id.container,  fragobj).commit();
    }

然后

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.displayText);
        String strtext = getArguments().getString("key");
        textView.setText(strtext);
        return rootView;
    }

这篇关于到TextView的分配值时,空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆