Kris
Kris

Reputation: 3779

Android: I'm getting a force close message when I modify main.xml file

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView android:text="@+id/customText"
android:id="@+id/customText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
</LinearLayout>

java file:

setContentView(R.layout.main);

final Handler mHandler = new Handler();
final TextView tv = (TextView) findViewById(R.id.customText);

tv.setText("Playing...");
setContentView(tv);

When I run this, I'm getting a force close message.

I'm new to android dev., many thanks in advance.. :)

Upvotes: 1

Views: 441

Answers (4)

yahya.abba
yahya.abba

Reputation: 1

Try to clean your project workspace: in Eclipse, click on Project --> Clean. This may be caused by previous errors in the XML file.

If you still have the error, open the XML file with Notepad, rectify it and try cleaning again.

Upvotes: 0

Aman Aalam
Aman Aalam

Reputation: 11251

As @Vladimir and @Chirag just said, you don't have to use setContentView() twice, and in your case, you already have set you activity's layout to main.xml

On the other note, I don't think you provide a reference to a TextView to the setContentView() method (as you've done in the last line of your java code), it is meant to be taking an id of a xml layout file only.

Upvotes: 0

Chirag
Chirag

Reputation: 56935

Please remove setContentView(tv); below tv.setText("Playing...");

Upvotes: 2

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43108

You don't need second setContentView();. Your TextView is already inflated to the activity.

Upvotes: 1

Related Questions