我在尝试给自定义组件充气时遇到错误。
之前使用的是使用LinearLayout开始的组件的xml,但它已经停止工作,因为我将其更改为com.example.football.on.uk.tv.CustomResultComponent。
我做了此更改,以遵循本教程的评论中提出的建议:http://www.anotherandroidblog.com/2011/05/26/custom-composite-android-component/
目前我并不想动态地夸大视图。我在XML的第24行收到错误,第一个是TextView。这个TextView似乎被正确定义,并且在我将此TextView的XML复制到另一个活动时起作用。
以下是我的自定义组件的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>