Android在表格行中添加文本视图

时间:2012-03-13 06:05:04

标签: android android-layout

好的我已将main.xml声明如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal" >

<RelativeLayout
    android:id="@+id/AdMob"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true" >
</RelativeLayout>

<ScrollView
    android:id="@+id/scrollview1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/AdMob" >

    <TableLayout
        android:id="@+id/tablelayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableRow
            android:id="@+id/tableRow0"
            android:layout_height="wrap_content"
            android:background="@drawable/banner" />

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/start_message"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:textSize="15sp" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/start"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="Start Now" >
            </Button>
        </TableRow>
    </TableLayout>
</ScrollView>

</RelativeLayout>

我的主要活动是

package com.michaelpeerman.demotivational_posters;

import com.michaelpeerman.demotivational_posters.R;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView start_message = (TextView) findViewById(R.id.start_message);
    start_message.setText(Html.fromHtml("Thank you for downloading \"Demotivational Posters\" \n Click start below to start. \n To advance to the next picture click the \"Next\" button. \n If you see an image you would like to download click the \"Download\" button. \n If you enjoy this app please leave a rating."));
}
}

当我去编译我的程序时,它强制自动关闭。 logcat输出是

03-13 01:59:50.240: E/AndroidRuntime(14336): FATAL EXCEPTION: main
03-13 01:59:50.240: E/AndroidRuntime(14336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michaelpeerman.demotivational_posters/com.michaelpeerman.demotivational_posters.MainActivity}: java.lang.ClassCastException: android.widget.TableRow cannot be cast to android.widget.TextView
03-13 01:59:50.240: E/AndroidRuntime(14336):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at android.os.Looper.loop(Looper.java:137)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at android.app.ActivityThread.main(ActivityThread.java:4424)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at java.lang.reflect.Method.invoke(Method.java:511)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at dalvik.system.NativeStart.main(Native Method)
03-13 01:59:50.240: E/AndroidRuntime(14336): Caused by: java.lang.ClassCastException: android.widget.TableRow cannot be cast to android.widget.TextView
03-13 01:59:50.240: E/AndroidRuntime(14336):    at com.michaelpeerman.demotivational_posters.MainActivity.onCreate(MainActivity.java:16)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at android.app.Activity.performCreate(Activity.java:4465)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-13 01:59:50.240: E/AndroidRuntime(14336):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-13 01:59:50.240: E/AndroidRuntime(14336):    ... 11 more

我无法弄清楚我将tablerow投放到textview

意味着什么

1 个答案:

答案 0 :(得分:1)

您需要再次清理并重建项目。如果这不起作用,则关闭项目并再次打开刷新项目并运行。我试着用你给出的代码。它适用于我的情况。