2012-08-14 205 views
0

我是新創建自定義組件。我正在通過擴展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> 

請幫助我,如果有人知道這個解決方案.. 在此先感謝..

+0

使用'LayoutInflater' – Praveenkumar 2012-08-14 06:01:19

+0

@SpK你能PLZ闡述yourself.How我用LayoutInflator? – 2012-08-14 06:12:36

回答

2

嘗試添加第二個構造函數。

public TextProgressBar(Context context, AttributeSet attrs) { 
    super(context, attrs); 

} 

看看是否有幫助:

+0

如何從我的Activity中調用此AttributeSet? – 2012-08-14 06:14:03

+0

我不認爲你需要調用它。你只需要擁有它。 – 0gravity 2012-08-14 06:16:57

+0

@nick無需從您的活動中調用此構造函數。只需將此構造函數添加到您的自定義類並運行即可。通過佈局XML文件加載組件時,將使用此構造函數。 – sunil 2012-08-14 06:18:32