目前我正在开发Android应用程序,我需要使用滑动抽屉 我正在关注tutorial。
当我尝试运行我的应用程序它给我一个错误。
通过分析logcat我发现了这个错误。
04-28 06:33:01.155: E/AndroidRuntime(5401): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.panel/org.panel.Test}: android.view.InflateException: Binary XML file line #56: Error inflating class org.panel.SmoothButton
这在Test.java的oncreate中显示错误
04-28 06:33:01.155: E/AndroidRuntime(5401): at org.panel.Test.onCreate(Test.java:30)
这显示了smoothButton.java中的错误
04-28 06:33:01.155: E/AndroidRuntime(5401): at org.panel.SmoothButton.<init>(SmoothButton.java:43)
在smoothButton.java的这个地方
if (transitionDrawable == null) {
throw new RuntimeException("transitionDrawable must be defined in XML (with valid xmlns)");
}
那么如何解决这个错误。谢谢你的帮助。
完成logcat
04-28 06:33:01.155: E/AndroidRuntime(5401): FATAL EXCEPTION: main
04-28 06:33:01.155: E/AndroidRuntime(5401): Process: org.panel, PID: 5401
04-28 06:33:01.155: E/AndroidRuntime(5401): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.panel/org.panel.Test}: android.view.InflateException: Binary XML file line #56: Error inflating class org.panel.SmoothButton
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.os.Handler.dispatchMessage(Handler.java:102)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.os.Looper.loop(Looper.java:136)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-28 06:33:01.155: E/AndroidRuntime(5401): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 06:33:01.155: E/AndroidRuntime(5401): at java.lang.reflect.Method.invoke(Method.java:515)
04-28 06:33:01.155: E/AndroidRuntime(5401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-28 06:33:01.155: E/AndroidRuntime(5401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-28 06:33:01.155: E/AndroidRuntime(5401): at dalvik.system.NativeStart.main(Native Method)
04-28 06:33:01.155: E/AndroidRuntime(5401): Caused by: android.view.InflateException: Binary XML file line #56: Error inflating class org.panel.SmoothButton
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-28 06:33:01.155: E/AndroidRuntime(5401): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.app.Activity.setContentView(Activity.java:1929)
04-28 06:33:01.155: E/AndroidRuntime(5401): at org.panel.Test.onCreate(Test.java:30)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.app.Activity.performCreate(Activity.java:5231)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-28 06:33:01.155: E/AndroidRuntime(5401): ... 11 more
04-28 06:33:01.155: E/AndroidRuntime(5401): Caused by: java.lang.reflect.InvocationTargetException
04-28 06:33:01.155: E/AndroidRuntime(5401): at java.lang.reflect.Constructor.constructNative(Native Method)
04-28 06:33:01.155: E/AndroidRuntime(5401): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
04-28 06:33:01.155: E/AndroidRuntime(5401): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
04-28 06:33:01.155: E/AndroidRuntime(5401): ... 23 more
04-28 06:33:01.155: E/AndroidRuntime(5401): Caused by: java.lang.RuntimeException: transitionDrawable must be defined in XML (with valid xmlns)
04-28 06:33:01.155: E/AndroidRuntime(5401): at org.panel.SmoothButton.<init>(SmoothButton.java:43)
04-28 06:33:01.155: E/AndroidRuntime(5401): ... 26 more
Test.java类
package org.panel;
import org.panel.Panel.OnPanelListener;
import easing.interpolator.BackInterpolator;
import easing.interpolator.BounceInterpolator;
import easing.interpolator.ElasticInterpolator;
import easing.interpolator.ExpoInterpolator;
import easing.interpolator.EasingType.Type;
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
public class Test extends Activity implements OnPanelListener {
private Panel bottomPanel;
private Panel topPanel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Panel panel;
topPanel = panel = (Panel) findViewById(R.id.topPanel);
panel.setOnPanelListener(this);
panel.setInterpolator(new BounceInterpolator(Type.OUT));
panel = (Panel) findViewById(R.id.leftPanel1);
panel.setOnPanelListener(this);
panel.setInterpolator(new BackInterpolator(Type.OUT, 2));
panel = (Panel) findViewById(R.id.leftPanel2);
panel.setOnPanelListener(this);
panel.setInterpolator(new BackInterpolator(Type.OUT, 2));
panel = (Panel) findViewById(R.id.rightPanel);
panel.setOnPanelListener(this);
panel.setInterpolator(new ExpoInterpolator(Type.OUT));
bottomPanel = panel = (Panel) findViewById(R.id.bottomPanel);
panel.setOnPanelListener(this);
panel.setInterpolator(new ElasticInterpolator(Type.OUT, 1.0f, 0.3f));
findViewById(R.id.smoothButton1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
bottomPanel.setOpen(!bottomPanel.isOpen(), true);
}
});
findViewById(R.id.smoothButton2).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
topPanel.setOpen(!topPanel.isOpen(), false);
}
});
}
public void onPanelClosed(Panel panel) {
String panelName = getResources().getResourceEntryName(panel.getId());
Log.d("Test", "Panel [" + panelName + "] closed");
}
public void onPanelOpened(Panel panel) {
String panelName = getResources().getResourceEntryName(panel.getId());
Log.d("Test", "Panel [" + panelName + "] opened");
}
}
这是SmoothButton.java
package org.panel;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;
public class SmoothButton extends Button {
private static final long DELAY = 25;
private LevelListDrawable transitionDrawable;
private int transitionDrawableLength;
private int level;
private int[] colors;
public SmoothButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SmoothButton);
transitionDrawable = (LevelListDrawable) a.getDrawable(R.styleable.SmoothButton_transitionDrawable);
transitionDrawableLength = a.getInt(R.styleable.SmoothButton_transitionDrawableLength, 0);
int useTextColors = 0;
int c0 = 0;
if (a.hasValue(R.styleable.SmoothButton_transitionTextColorUp)) {
c0 = a.getColor(R.styleable.SmoothButton_transitionTextColorUp, 0);
useTextColors++;
}
int c1 = 0;
if (useTextColors == 1 && a.hasValue(R.styleable.SmoothButton_transitionTextColorDown)) {
c1 = a.getColor(R.styleable.SmoothButton_transitionTextColorDown, 0);
useTextColors++;
}
a.recycle();
if (transitionDrawable == null) {
throw new RuntimeException("transitionDrawable must be defined in XML (with valid xmlns)");
}
if (transitionDrawableLength == 0) {
throw new RuntimeException("transitionDrawableLength must be defined in XML (with valid xmlns)");
}
if (useTextColors == 2) {
setTextColor(c0);
int a0 = Color.alpha(c0);
int r0 = Color.red(c0);
int g0 = Color.green(c0);
int b0 = Color.blue(c0);
int a1 = Color.alpha(c1);
int r1 = Color.red(c1);
int g1 = Color.green(c1);
int b1 = Color.blue(c1);
colors = new int[transitionDrawableLength];
for (int i=0; i<transitionDrawableLength; i++) {
int ai = a0 + i * (a1 - a0) / transitionDrawableLength;
int ri = r0 + i * (r1 - r0) / transitionDrawableLength;
int gi = g0 + i * (g1 - g0) / transitionDrawableLength;
int bi = b0 + i * (b1 - b0) / transitionDrawableLength;
colors[i] = Color.argb(ai, ri, gi, bi);
}
}
level = 0;
transitionDrawable.setLevel(level);
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();
int paddingRight = getPaddingRight();
int paddingBottom = getPaddingBottom();
setBackgroundDrawable(transitionDrawable);
setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
int delta = isPressed()? 1 : -1;
handler.removeMessages(-delta);
handler.sendEmptyMessage(delta);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int what = msg.what;
level += what;
if (level >= 0 && level < transitionDrawableLength) {
transitionDrawable.setLevel(level);
if (colors != null) {
setTextColor(colors[level]);
}
handler.sendEmptyMessageDelayed(what, DELAY);
} else {
level = Math.max(0, level);
level = Math.min(transitionDrawableLength-1, level);
}
}
};
public void setTransitionDrawable(Drawable drawable, int length) {
transitionDrawable = (LevelListDrawable) drawable;
transitionDrawableLength = length;
level = 0;
invalidate();
}
}
这是main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:panel="http://schemas.android.com/apk/src/org.panel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#767c9b">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<org.panel.Panel
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/topPanel"
android:paddingBottom="20dip"
panel:position="top"
panel:animationDuration="1000"
panel:linearFlying="true"
panel:openedHandle="@drawable/top_switcher_expanded_background"
panel:closedHandle="@drawable/top_switcher_collapsed_background"
>
<Button
android:id="@id/panelHandle"
android:layout_width="fill_parent"
android:layout_height="33dip"
/>
<LinearLayout
android:id="@id/panelContent"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="60dip"
android:gravity="center"
android:text="top check box"
android:background="#688"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Bounce\nInterpolator"
android:textSize="16dip"
android:padding="4dip"
android:textColor="#eee"
android:textStyle="bold"
android:background="#323299"
/>
</LinearLayout>
</org.panel.Panel>
<org.panel.SmoothButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/smoothButton1"
android:text="click me and see bottom panel..."
android:textStyle="bold"
panel:transitionDrawable="@drawable/transition_list"
panel:transitionDrawableLength="8"
panel:transitionTextColorUp="#eee"
panel:transitionTextColorDown="#aaa"
/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="16dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:paddingLeft="45dip"
android:paddingRight="45dip"
android:textColor="#ddd"
android:text="Central area\n\nTry to use panels on each side of me\n\nEvery panel has its own interpolator\n\nLeft/Right panels are overlayed"
/>
</ScrollView>
<org.panel.SmoothButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/smoothButton2"
android:text="click me and see top panel..."
android:textStyle="bold"
panel:transitionDrawable="@drawable/transition_list"
panel:transitionDrawableLength="8"
panel:transitionTextColorUp="#eee"
panel:transitionTextColorDown="#aaa"
/>
<org.panel.Panel
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bottomPanel"
android:paddingTop="20dip"
panel:position="bottom"
panel:animationDuration="1200"
panel:openedHandle="@drawable/bottom_switcher_expanded_background"
panel:closedHandle="@drawable/bottom_switcher_collapsed_background"
>
<Button
android:id="@id/panelHandle"
android:layout_width="fill_parent"
android:layout_height="33dip"
/>
<LinearLayout
android:id="@id/panelContent"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Elastic\nInterpolator"
android:textSize="16dip"
android:padding="4dip"
android:textColor="#eee"
android:textStyle="bold"
android:background="#993333"
/>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="60dip"
android:gravity="center"
android:text="bottom check box"
android:background="#688"
/>
</LinearLayout>
</org.panel.Panel>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<org.panel.Panel
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/leftPanel1"
android:layout_weight="1"
android:layout_gravity="left"
panel:position="left"
panel:openedHandle="@drawable/left_switcher_expanded_background"
panel:closedHandle="@drawable/left_switcher_collapsed_background"
>
<Button
android:id="@id/panelHandle"
android:layout_width="33dip"
android:layout_height="fill_parent"
/>
<TextView
android:id="@id/panelContent"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="4dip"
android:text="This is\nmy first\nleft Panel"
android:textSize="16dip"
android:textColor="#eee"
android:textStyle="bold"
android:background="#999932"
/>
</org.panel.Panel>
<org.panel.Panel
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/leftPanel2"
android:layout_weight="1"
android:layout_gravity="left"
panel:position="left"
panel:openedHandle="@drawable/left_switcher_expanded_background"
panel:closedHandle="@drawable/left_switcher_collapsed_background"
>
<Button
android:id="@id/panelHandle"
android:layout_width="33dip"
android:layout_height="fill_parent"
/>
<TextView
android:id="@id/panelContent"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="4dip"
android:text="This is\nmy second\nleft Panel"
android:textSize="16dip"
android:textColor="#eee"
android:textStyle="bold"
android:background="#115544"
/>
</org.panel.Panel>
</LinearLayout>
<org.panel.Panel
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/rightPanel"
android:layout_gravity="right"
panel:position="right"
panel:openedHandle="@drawable/right_switcher_expanded_background"
panel:closedHandle="@drawable/right_switcher_collapsed_background"
>
<Button
android:id="@id/panelHandle"
android:layout_width="33dip"
android:layout_height="fill_parent"
/>
<TextView
android:id="@id/panelContent"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="4dip"
android:text="Expo\nInterpolator"
android:textSize="16dip"
android:textColor="#eee"
android:textStyle="bold"
android:background="#329999"
/>
</org.panel.Panel>
</FrameLayout>