0

在我正在开发的应用程序中,我有一个带有图标的菜单,它位于屏幕的左右边缘,并且它在所有应用程序的活动中都存在。

我所做的是首先创建“MenuView”类(可以扩展 View、RelativeLayout 或其他东西),其中定义了所有菜单的图标及其 onClickListeners。然后是这个类的布局。在布局中,我建立了 2 个线性布局,每个布局都位于屏幕边框上(这些是指菜单)和一个具有列内空白区域的线性布局,其他活动必须在其中进行。

MenuViewActivity.java

public class MenuViewActivity extends {

public MenuViewActivity(Context context, AttributeSet attrs) {
    super(context, attrs);
    ...

    ((ImageView)this.findViewById(R.id.navButton)).setOnClickListener(launch_nav);

    final OnClickListener launch_nav = new OnClickListener() {
        @Override
        public void onClick(View v) {
            getBaseContext().startActivity(new Intent(getBaseContext(), Navigation.class));
        }
    };
    ...

menu_view.xml

<RelativeLayout     
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
tools:context="com.example.MenuViewActivity" >


<!-- Shows custom time titlebar -->
<include 
    layout="@layout/custom_tittlebar" >
</include>


<!-- Button columns -->    
<LinearLayout 
    android:id="@+id/lefthandmenu"
    android:layout_width="85dip"
    android:layout_height="match_parent"
    android:layout_below="@id/title"
    android:layout_alignParentLeft="true"
    android:orientation="vertical"       
    android:background="@drawable/border_cut" >

    <ImageView
        ... />
    ...
</LinearLayout>

<LinearLayout 
    android:id="@+id/righthandmenu"
    android:layout_width="85dip"
    android:layout_height="match_parent"
    android:layout_below="@id/title"
    android:layout_alignParentRight="true"
    android:orientation="vertical"
    android:background="@drawable/border_cut" >

    <ImageView
        ... />
    ...        
</LinearLayout>

<!-- Blank space which will contain other activities -->
<LinearLayout
    android:id="@+id/activitycontent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toLeftOf="@id/righthandmenu"
    android:layout_toRightOf="@id/lefthandmenu"
    android:layout_below="@id/title"
    android:orientation="vertical" >    
</LinearLayout>   

所以,我现在必须做的是告诉程序其他活动必须进入最后一个布局。为此,我在例如 Main 类中执行此操作。我夸大了布局。

主类.java

public class MainClass extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);


    LinearLayout inside_menu_view = (LinearLayout)findViewById(R.id.activitycontent);
    View this_layout = getLayoutInflater().inflate(R.layout.main, inside_menu_view);
    inside_menu_view.addView(this_layout);

但是我在 logcat 上遇到错误:

更新 - 修改后仍然无法正常工作,这是 LogCat

10-31 09:35:17.521: E/AndroidRuntime(1676): FATAL EXCEPTION: main
10-31 09:35:17.521: E/AndroidRuntime(1676): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainClass}: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.MenuViewActivity
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.os.Looper.loop(Looper.java:130)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at java.lang.reflect.Method.invokeNative(Native Method)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at java.lang.reflect.Method.invoke(Method.java:507)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at dalvik.system.NativeStart.main(Native Method)
10-31 09:35:17.521: E/AndroidRuntime(1676): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.MenuViewActivity
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at com.masermic.medialauncher.LauncherActivity.onCreate(LauncherActivity.java:67)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-31 09:35:17.521: E/AndroidRuntime(1676):     ... 11 more
10-31 09:35:17.521: E/AndroidRuntime(1676): Caused by: java.lang.reflect.InvocationTargetException
10-31 09:35:17.521: E/AndroidRuntime(1676):     at java.lang.reflect.Constructor.constructNative(Native Method)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
10-31 09:35:17.521: E/AndroidRuntime(1676):     at android.view.LayoutInflater.createView(LayoutInflater.java:505)
10-31 09:35:17.521: E/AndroidRuntime(1676):     ... 19 more
10-31 09:35:17.521: E/AndroidRuntime(1676): Caused by: java.lang.NullPointerException
10-31 09:35:17.521: E/AndroidRuntime(1676):     at com.mexample.MenuViewActivity.<init>(MenuViewActivity.java:55)
10-31 09:35:17.521: E/AndroidRuntime(1676):     ... 22 more
4

1 回答 1

0

你不能充气Activity。您可以膨胀View和派生类型。View因此,您的类应该 extendsViewGroup或一些派生类。

编辑:

关于您的膨胀异常,您在自定义视图的第 55 行获得了 NPE,可能是因为R.id.navButton未在您试图膨胀的布局内声明。

于 2013-10-31T09:23:21.090 回答