這是我的主要活動類。
package com.example.parth.customcomposite; import android.app.Activity; import android.os.Bundle;
public class CustomCompositeEx extends Activity {
private TwoTextWidget itemOne; private TwoTextWidget iteamTwo;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private void setupViews()
{
itemOne=(TwoTextWidget)findViewById(R.id.list_item_one);
itemOne.setTextOne("1.");
itemOne.setTextTwo("Hi");
iteamTwo.setTextTwo("2.");
iteamTwo.setTextTwo("Hello");
}
}
這是我的自定義類。
package com.example.parth.customcomposite;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TwoTextWidget extends LinearLayout {
public TwoTextWidget(Context context, AttributeSet attrs) { super(context, attrs); }
private TextView textone; private TextView texttwo;
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.twotext, this);
setItems();
}
private void setItems()
{
textone=(TextView)findViewById(R.id.text_one);
texttwo=(TextView)findViewById(R.id.text_two);
}
public void setTextOne(String text)
{
textone.setText(text);
}
public void setTextTwo(String text)
{
texttwo.setText(text);
}
我的自定義視圖XML是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
></TextView>
<TextView
android:id="@+id/text_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"
></TextView>
</LinearLayout>
我的主XML ISL:
我的清單文件:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CustomCompositeEx"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
難道你還張貼堆棧跟蹤?錯誤發生在哪裏? – BadSkillz 2011-06-14 06:39:47
而且?爲什麼它無法啓動活動?請發佈您的logcat一次,並指向拋出異常的具體行。 – Egor 2011-06-14 06:41:01