我是新創建自定義組件。我正在通過擴展ProgressBar來創建自定義進度欄。但得到這個例外。自定義進度條在android
07-07 17:38:00.700: D/AndroidRuntime(3513): Shutting down VM
07-07 17:38:00.700: W/dalvikvm(3513): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-07 17:38:00.720: E/AndroidRuntime(3513): FATAL EXCEPTION: main
07-07 17:38:00.720: E/AndroidRuntime(3513): android.view.InflateException: Binary XML file line #7: Error inflating class com.customprogress.TextProgressBar
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-07 17:38:00.720: E/AndroidRuntime(3513): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.app.Activity.setContentView(Activity.java:1647)
07-07 17:38:00.720: E/AndroidRuntime(3513): at com.customprogress.TextProgressBar.<init>(TextProgressBar.java:22)
07-07 17:38:00.720: E/AndroidRuntime(3513): at com.customprogress.Progress$Click.onClick(Progress.java:27)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.view.View.performClick(View.java:2408)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.view.View$PerformClick.run(View.java:8816)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.os.Handler.handleCallback(Handler.java:587)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.os.Looper.loop(Looper.java:123)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-07 17:38:00.720: E/AndroidRuntime(3513): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 17:38:00.720: E/AndroidRuntime(3513): at java.lang.reflect.Method.invoke(Method.java:521)
07-07 17:38:00.720: E/AndroidRuntime(3513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-07 17:38:00.720: E/AndroidRuntime(3513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-07 17:38:00.720: E/AndroidRuntime(3513): at dalvik.system.NativeStart.main(Native Method)
07-07 17:38:00.720: E/AndroidRuntime(3513): Caused by: java.lang.NoSuchMethodException: TextProgressBar(Context,AttributeSet)
07-07 17:38:00.720: E/AndroidRuntime(3513): at java.lang.Class.getMatchingConstructor(Class.java:660)
07-07 17:38:00.720: E/AndroidRuntime(3513): at java.lang.Class.getConstructor(Class.java:477)
07-07 17:38:00.720: E/AndroidRuntime(3513): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
07-07 17:38:00.720: E/AndroidRuntime(3513): ... 20 more
我的創造,這是這裏代碼.. TextProgressBar.class
public class TextProgressBar extends ProgressBar {
Activity activity;
public TextProgressBar(Activity activity) {
super(activity);
this.activity = activity;
this.activity.setContentView(R.layout.custom_progress);
}
而且我從我的Progress.java活動
public class Progress extends Activity {
/** Called when the activity is first created. */
Button buttonClick;
TextProgressBar textProgressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new Click());
}
class Click implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textProgressBar = new TextProgressBar(Progress.this);
}
}
}
的XML調用這個類custom_progress被這裏..
<?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="wrap_content"
android:orientation="vertical" >
<com.customprogress.TextProgressBar
android:id="@+id/progressBarWithText"
android:layout_width="100dp"
android:layout_height="100dp"
/>
</LinearLayout>
請幫助我,如果有人知道這個解決方案.. 在此先感謝..
使用'LayoutInflater' – Praveenkumar 2012-08-14 06:01:19
@SpK你能PLZ闡述yourself.How我用LayoutInflator? – 2012-08-14 06:12:36