我是Android的新手,我正试图在我的Android应用程序的许多活动中为一个普通的页脚充气。页脚中的某些按钮需要在应用程序中同样工作。这是我的代码:
BottomBar类:
package com.traderscockpit;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
public class BottomBar extends RelativeLayout {
private LayoutInflater inflater;
public BottomBar(Context context, AttributeSet attr) {
super(context, attr);
// TODO Auto-generated constructor stub
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.bottom_bar, this, true);
}
}
bottom_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/homeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:layout_weight="1"
android:onClick="HomeClick"
/>
<Button
android:layout_weight="1"
android:id="@+id/logoutButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
android:onClick="HomeClick"
/>
</merge>
这就是我将其与活动合并的方式:
<?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"
android:padding="0dp"
>
<ListView
android:id="@+id/menu"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</ListView>
<com.traderscockpit.inflating.BottomBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
</LinearLayout>
这是我得到的堆栈跟踪:
09-22 00:26:26.454: E/AndroidRuntime(3632): FATAL EXCEPTION: main
09-22 00:26:26.454: E/AndroidRuntime(3632): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.traderscockpit/com.traderscockpit.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class com.traderscockpit.inflating.BottomBar
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.os.Looper.loop(Looper.java:137)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-22 00:26:26.454: E/AndroidRuntime(3632): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 00:26:26.454: E/AndroidRuntime(3632): at java.lang.reflect.Method.invoke(Method.java:525)
09-22 00:26:26.454: E/AndroidRuntime(3632): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-22 00:26:26.454: E/AndroidRuntime(3632): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 00:26:26.454: E/AndroidRuntime(3632): at dalvik.system.NativeStart.main(Native Method)
09-22 00:26:26.454: E/AndroidRuntime(3632): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.traderscockpit.inflating.BottomBar
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-22 00:26:26.454: E/AndroidRuntime(3632): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.app.Activity.setContentView(Activity.java:1895)
09-22 00:26:26.454: E/AndroidRuntime(3632): at com.traderscockpit.MainActivity.onCreate(MainActivity.java:46)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.app.Activity.performCreate(Activity.java:5133)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-22 00:26:26.454: E/AndroidRuntime(3632): ... 11 more
09-22 00:26:26.454: E/AndroidRuntime(3632): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.traderscockpit.inflating.BottomBar" on path: DexPathList[[zip file "/data/app/com.traderscockpit-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.traderscockpit-1, /system/lib]]
09-22 00:26:26.454: E/AndroidRuntime(3632): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
09-22 00:26:26.454: E/AndroidRuntime(3632): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-22 00:26:26.454: E/AndroidRuntime(3632): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.view.LayoutInflater.createView(LayoutInflater.java:559)
09-22 00:26:26.454: E/AndroidRuntime(3632): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
到目前为止我还没有附上事件处理程序,不知道问题出在哪里。请帮忙。提前谢谢。
答案 0 :(得分:2)
xml中的包中有拼写错误。它应该是com.traderscockpit.BottomBar
,而不是com.traderscockpit.inflating.BottomBar
答案 1 :(得分:0)
BottomBar
类位于com.traderscockpit
包中,但您在xml文件中将其定义为com.traderscockpit.inflating