0

我是 android 新手,我正在尝试在我的 android 应用程序的许多活动中添加一个通用页脚。页脚中的一些按钮需要在整个应用程序中工作。这是我的代码:

底栏类:

 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)

直到现在我还没有附加事件处理程序,不知道问题出在哪里。请帮忙。提前致谢。

4

2 回答 2

2

您在 xml 中的包中有错字。应该是com.traderscockpit.BottomBar,不是com.traderscockpit.inflating.BottomBar

于 2013-10-17T15:11:57.603 回答
0

BottomBar类在com.traderscockpit包中,但您在 xml 文件中将其定义为com.traderscockpit.inflating

于 2013-10-17T15:15:14.063 回答