当我尝试在 main.xml 中使用自定义搜索栏时,我无法实例化错误消息,并且在运行时它会给出错误膨胀类消息。
海关搜索栏.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"
android:orientation="vertical" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/leftText"
android:textSize="10dp"
android:gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/centerText"
android:textSize="20dp"
android:gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/rightText"
android:textSize="10dp"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
CustomSeekBar.java
package kirbz.Component.CustomSeekBar;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.SeekBar;
import android.widget.TextView;
public class CustomSeekBar extends SeekBar {
private SeekBar mSeekBar = (SeekBar) findViewById(R.id.seekBar);
private TextView mMinText = (TextView) findViewById(R.id.leftText);
private TextView mMaxText = (TextView) findViewById(R.id.rightText);
private TextView mValueText = (TextView) findViewById(R.id.centerText);
public CustomSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
mSeekBar=(SeekBar) findViewById(R.id.seekBar);
mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mValueText.setText(String.valueOf(new Integer(progress)));
}
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
mValueText.setText(String.valueOf(new Integer(mSeekBar.getProgress())));
}
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
mValueText.setText(String.valueOf(new Integer(mSeekBar.getProgress())));
}
});
// TODO Auto-generated constructor stub
}
public void setValues(int max, int min, int value) {
mSeekBar.setMax(max-min);
mMaxText.setText(String.valueOf(max));
mMinText.setText(String.valueOf(min));
mSeekBar.setProgress(value - min);
mValueText.setText(String.valueOf(value));
}
}
我的 main.xml 是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<kirbz.Component.CustomSeekBar.CustomSeekBar
android:id="@+id/customSeekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left" />
LogCAT
05-01 17:39:51.227: E/AndroidRuntime(675): FATAL EXCEPTION: main
05-01 17:39:51.227: E/AndroidRuntime(675): java.lang.RuntimeException: Unable to start activity ComponentInfo{kirbz.Component.CustomSeekBar/kirbz.Component.CustomSeekBar.CustomSeekBarActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class kirbz.Component.CustomSeekBar.CustomSeekBar
05-01 17:39:51.227: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.os.Looper.loop(Looper.java:123)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-01 17:39:51.227: E/AndroidRuntime(675): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 17:39:51.227: E/AndroidRuntime(675): at java.lang.reflect.Method.invoke(Method.java:507)
05-01 17:39:51.227: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-01 17:39:51.227: E/AndroidRuntime(675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-01 17:39:51.227: E/AndroidRuntime(675): at dalvik.system.NativeStart.main(Native Method)
05-01 17:39:51.227: E/AndroidRuntime(675): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class kirbz.Component.CustomSeekBar.CustomSeekBar
05-01 17:39:51.227: E/AndroidRuntime(675): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-01 17:39:51.227: E/AndroidRuntime(675): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.app.Activity.setContentView(Activity.java:1657)
05-01 17:39:51.227: E/AndroidRuntime(675): at kirbz.Component.CustomSeekBar.CustomSeekBarActivity.onCreate(CustomSeekBarActivity.java:11)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-01 17:39:51.227: E/AndroidRuntime(675): ... 11 more
05-01 17:39:51.227: E/AndroidRuntime(675): Caused by: java.lang.reflect.InvocationTargetException
05-01 17:39:51.227: E/AndroidRuntime(675): at java.lang.reflect.Constructor.constructNative(Native Method)
05-01 17:39:51.227: E/AndroidRuntime(675): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
05-01 17:39:51.227: E/AndroidRuntime(675): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
05-01 17:39:51.227: E/AndroidRuntime(675): ... 21 more
05-01 17:39:51.227: E/AndroidRuntime(675): Caused by: java.lang.NullPointerException
05-01 17:39:51.227: E/AndroidRuntime(675): at kirbz.Component.CustomSeekBar.CustomSeekBar.<init>(CustomSeekBar.java:18)
05-01 17:39:51.227: E/AndroidRuntime(675): ... 24 more
你能告诉我我错过了什么吗?
感谢您的时间,并提前感谢。