3

尝试为我的自定义组件充气时出现错误。

它以前使用以 LinearLayout 开头的组件的 xml,但自从我将其更改为 com.example.football.on.uk.tv.CustomResultComponent 后它已停止工作。

我进行了此更改以遵循本教程评论中的建议:http ://www.anotherandroidblog.com/2011/05/26/custom-composite-android-component/

目前我并没有试图动态地膨胀视图。我的 XML 第 24 行(第一个 TextView)出现错误。当我将此 TextView 的 XML 复制到另一个活动中时,此 TextView 似乎已正确定义并且可以正常工作。

这是我的自定义组件的 xml 定义:

<?xml version="1.0" encoding="utf-8"?>
<com.example.football.on.uk.tv.CustomResultComponent
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/Black"
        android:paddingLeft="2dip"
        android:paddingRight="2dip"
        android:paddingTop="1dip" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <TextView
                android:id="@+id/tvHomeTeam"
                android:layout_width="140dip"
                android:layout_height="fill_parent"
                android:background="@color/White"
                android:gravity="right"
                android:text="HomeTeam" />

            <TextView
                android:id="@+id/tvVersus"
                android:layout_width="40dp"
                android:layout_height="fill_parent"
                android:background="@color/White"
                android:gravity="center"
                android:text="  -  " />

            <TextView
                android:id="@+id/tvAwayTeam"
                android:layout_width="140dip"
                android:layout_height="fill_parent"
                android:background="@color/White"
                android:text="Away Team" />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/Black"
        android:paddingBottom="1dip"
        android:paddingLeft="2dip"
        android:paddingRight="2dip" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <TextView
                android:id="@+id/tvDateTime"
                android:layout_width="130dip"
                android:layout_height="wrap_content"
                android:background="@color/White"
                android:gravity="center_horizontal"
                android:text=" 01/01/1950    12:45" />

            <TextView
                android:id="@+id/tvCompetition"
                android:layout_width="140dip"
                android:layout_height="wrap_content"
                android:background="@color/White"
                android:gravity="center_horizontal"
                android:text="Premier League" />

            <ImageView
                android:id="@+id/ivChannel"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:background="@color/White"
                android:paddingBottom="4dip"
                android:paddingLeft="4dip"
                android:paddingRight="4dip"
                android:paddingTop="4dip"
                android:src="@drawable/logo_espn" />
        </TableRow>
    </TableLayout>
</LinearLayout>

</com.example.football.on.uk.tv.CustomResultComponent>

这是该组件的类:

package com.example.football.on.uk.tv;

import android.app.Activity;

import com.example.football.on.uk.tv.R;

import android.content.Context;
import android.text.Layout;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomResultComponent extends LinearLayout{
private TextView tvHomeTeam;
private TextView tvVersus;
private TextView tvAwayTeam;
private TextView tvDateTime;
private TextView tvCompetition;
private ImageView ivChannel;

public CustomResultComponent(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onFinishInflate(){
    super.onFinishInflate();
        ((Activity)getContext()).getLayoutInflater().inflate(R.layout.custom_result_component, this);
    setupViewItems();
}

private void setupViewItems() {
    // TODO Auto-generated method stub
    tvHomeTeam = (TextView) findViewById(R.id.tvHomeTeam);
    tvVersus = (TextView) findViewById(R.id.tvVersus);
    tvAwayTeam = (TextView) findViewById(R.id.tvAwayTeam);
    tvDateTime = (TextView) findViewById(R.id.tvDateTime);
    tvCompetition = (TextView) findViewById(R.id.tvCompetition);
    ivChannel = (ImageView) findViewById(R.id.ivChannel);
}

public void setHomeTeam(String team){
    tvHomeTeam.setText("Tottenham");
}
}

这是我希望自定义组件出现的活动的 XML:

<LinearLayout 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:background="@color/LightBlue"
android:id="@+id/AboutLayout"
android:orientation="vertical">

<com.example.football.on.uk.tv.CustomResultComponent
    android:id="@+id/customResultComponent1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</com.example.football.on.uk.tv.CustomResultComponent>

</LinearLayout>

这是活动:

package com.example.football.on.uk.tv;

import android.os.Bundle;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

import com.example.football.on.uk.tv.CustomResultComponent;

public class About extends Activity {



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);



}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_about, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

}

最后这是我收到的错误消息:

为很长的帖子道歉!我想提供尽可能多的信息。

09-29 07:53:45.612: E/AndroidRuntime(20547): FATAL EXCEPTION: main
09-29 07:53:45.612: E/AndroidRuntime(20547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.football.on.uk.tv/com.example.football.on.uk.tv.About}: android.view.InflateException: Binary XML file line #24: Error inflating class <unknown>
09-29 07:53:45.612: E/AndroidRuntime(20547):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at android.os.Looper.loop(Looper.java:130)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at android.app.ActivityThread.main(ActivityThread.java:3687)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at java.lang.reflect.Method.invokeNative(Native Method)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at java.lang.reflect.Method.invoke(Method.java:507)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-29 07:53:45.612: E/AndroidRuntime(20547):    at dalvik.system.NativeStart.main(Native Method)
09-29 07:53:45.612: E/AndroidRuntime(20547): Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class <unknown>
4

0 回答 0