当我尝试在main.xml中使用自定义搜索栏时,我无法实例化错误消息,并且在运行时它会给出一个Error inflating类消息。
customseekbar.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
你能告诉我我错过了什么吗?
感谢您的时间并提前致谢。
答案 0 :(得分:1)
我改变了整个方法,并设法通过以下方式实现我想要的目标 -
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:gravity="left" android:layout_gravity="left|center_vertical" android:layout_weight="1" android:paddingLeft="10dp"/>
<TextView
android:id="@+id/centerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center" android:layout_weight="1"/>
<TextView
android:id="@+id/rightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:gravity="right" android:layout_weight="1" android:paddingRight="10dp"/>
</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>
</LinearLayout>
CustomBar.java是一个自定义类,它将布局膨胀为LAYOUT
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.view.View;
import android.widget.TextView;
public class CustomBar extends LinearLayout {
private SeekBar mSeekBar;
private TextView mMinText;
private TextView mMaxText;
private TextView mValueText;
public CustomBar(Context context, AttributeSet attrs) {
super(context, attrs);
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.customseekbarlayout, this);
initializeLayout();
// 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));
}
private void initializeLayout() {
this.mSeekBar = (SeekBar) findViewById(R.id.seekBar);
this.mMinText = (TextView) findViewById(R.id.leftText);
this.mMaxText = (TextView) findViewById(R.id.rightText);
this.mValueText = (TextView) findViewById (R.id.centerText);
this.mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
mValueText.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//Toast.makeText(getContext(), "Test2", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//Toast.makeText(getContext(), "Test3", Toast.LENGTH_SHORT).show();
}
});
}
public int getValue(){
return mSeekBar.getProgress();
}
}
main.xml是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<kirbz.Components.CustomSeekBar.CustomBar
android:id="@+id/customBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</kirbz.Components.CustomSeekBar.CustomBar>
</LinearLayout>
并且MainActivity是
import android.app.Activity;
import android.os.Bundle;
public class CustomSeekBarActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final CustomBar mSeekBar = (CustomBar) findViewById(R.id.customBar1);
mSeekBar.setValues(8,4,6);
}
}