我创建了SeekBar类的扩展名:
package com.simplemathgame;
import android.content.Context;
import android.widget.SeekBar;
import android.widget.TextView;
public class SeekBarPlus extends SeekBar {
private TextView numberOfDrills;
public SeekBarPlus(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
numberOfDrills.setText(progress);
}
public void setTextView(TextView textView){
numberOfDrills = textView;
}
}
这是xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000044">
<TableRow
android:layout_marginTop="10dp"
android:gravity="center_vertical" >
<TextView
android:textColor="#FFFFFFFF"
android:textSize="16sp"
android:textStyle="bold"
android:text="Addition Drills:"/>
</TableRow>
<TableRow
android:layout_marginTop="5dp"
android:gravity="center_vertical" >
<com.simplemathgame.SeekBarPlus
android:id="@+id/add_seek_bar"
android:layout_width="20dp"
android:max="10"
android:layout_weight="0.8"/>
<TextView
android:id="@+id/add_drills_number"
android:textColor="#FFFFFFFF"
android:textSize="16sp"
android:textStyle="bold"
android:layout_weight="0.2"
android:gravity="center_horizontal"
android:text="0"/>
</TableRow>
</TableLayout>
这是logCat:
12-19 19:52:48.457: D/dalvikvm(335): GC_EXTERNAL_ALLOC freed 46K, 53% free 2546K/5379K, external 1917K/2137K, paused 72ms
12-19 19:52:51.617: D/AndroidRuntime(335): Shutting down VM
12-19 19:52:51.617: W/dalvikvm(335): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-19 19:52:51.647: E/AndroidRuntime(335): FATAL EXCEPTION: main
12-19 19:52:51.647: E/AndroidRuntime(335): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplemathgame/com.simplemathgame.MainActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class com.simplemathgame.SeekBarPlus
12-19 19:52:51.647: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.os.Looper.loop(Looper.java:123)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-19 19:52:51.647: E/AndroidRuntime(335): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 19:52:51.647: E/AndroidRuntime(335): at java.lang.reflect.Method.invoke(Method.java:507)
12-19 19:52:51.647: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-19 19:52:51.647: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-19 19:52:51.647: E/AndroidRuntime(335): at dalvik.system.NativeStart.main(Native Method)
12-19 19:52:51.647: E/AndroidRuntime(335): Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class com.simplemathgame.SeekBarPlus
12-19 19:52:51.647: E/AndroidRuntime(335): at android.view.LayoutInflater.createView(LayoutInflater.java:508)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-19 19:52:51.647: E/AndroidRuntime(335): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.app.Activity.setContentView(Activity.java:1657)
12-19 19:52:51.647: E/AndroidRuntime(335): at com.simplemathgame.MainActivity.onCreate(MainActivity.java:16)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-19 19:52:51.647: E/AndroidRuntime(335): ... 11 more
12-19 19:52:51.647: E/AndroidRuntime(335): Caused by: java.lang.NoSuchMethodException: SeekBarPlus(Context,AttributeSet)
12-19 19:52:51.647: E/AndroidRuntime(335): at java.lang.Class.getMatchingConstructor(Class.java:643)
12-19 19:52:51.647: E/AndroidRuntime(335): at java.lang.Class.getConstructor(Class.java:472)
12-19 19:52:51.647: E/AndroidRuntime(335): at android.view.LayoutInflater.createView(LayoutInflater.java:480)
12-19 19:52:51.647: E/AndroidRuntime(335): ... 22 more
我看到扩展的SeekBarPlus类存在问题,但不知道为什么。谢谢!
答案 0 :(得分:6)
您未在自定义视图类中指定完整的构造函数集。在您的特定情况下,SeekBarPlus(Context,AttributeSet)
构造函数丢失。
自定义视图类需要具有以下3个构造函数:
public SeekBarPlus(Context context) {
super(context);
}
public SeekBarPlus(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SeekBarPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
如果您不需要它们,请按照上面的说明调用super()
。
答案 1 :(得分:0)
你错过了实现View提供的另外两个cunstructors:
// Constructor that is called when inflating a view from XML.
View(Context context, AttributeSet attrs)
// Perform inflation from XML and apply a class-specific base style.
View(Context context, AttributeSet attrs, int defStyle)