我是创建自定义组件的新手。我通过扩展 ProgressBar 来创建自定义进度栏。但得到这个例外。
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);
}
}
}
custom_progress的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="wrap_content"
android:orientation="vertical" >
<com.customprogress.TextProgressBar
android:id="@+id/progressBarWithText"
android:layout_width="100dp"
android:layout_height="100dp"
/>
</LinearLayout>
如果有人知道这个的解决方案,请帮助我.. 提前谢谢..
答案 0 :(得分:2)
尝试添加第二个构造函数。
public TextProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
看看是否有帮助: