开发者

Pass TextView to an Intent in Android

In new project in Android I have a layout with a TextView and a TabWidget in the main.xml file.

My tab menu launches different activities using Intents, in these activities I need to do:

TextView title = (TextView)findViewById(R.id.titolo);
        title.setText("Home");

but I can do this only in the main activity while if I try to do this in other activities I get a nullpointer exception.. What is the problem?

In the manifest I inserted other activities li开发者_运维知识库ke this, is this the problem?

<activity android:name="List1"></activity>

I tryed using

LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = li.inflate(R.layout.main, null);
TextView title = (TextView)row.findViewById(R.id.titolo);

I get no errors but the title didn't change


A TextView with that ID needs to be present in all your activity layouts. You are getting a NullPointerException as Android can not find the view. Also make sure you are setting the layout in onCreate properly using setContentView.


The TextView with this id (titolo) must be a child of the layout of the Activity where you use this code.

TextView title = (TextView)findViewById(R.id.titolo);
if(title != null)
    title.setText("Home");
else
    Log.e(TAG, "title TextView not found in current layout);

If you'r trying to implement a Actionbar of some sort, try creating a "actionbar.xml" (containing the TextView with the id "titolo") for example and include it in all your layouts.


Your manifest should be as

<activity android:name=".List1"></activity> 

In List1 activity

setContentView(set_layout_which_contains_titolo) and then use 

TextView title = (TextView)findViewById(R.id.titolo);
        title.setText("Home");

But if your work may be done as to send string (which contains value of text view) to calling activity. It will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜