2013-10-31 149 views
0

在我正在開發的應用程序中,我有一個帶有圖標的菜單,它位於屏幕的右側和左側邊緣,它在所有應用程序的活動中保留在那裏。在LinearLayout中膨脹佈局

我所做的是首先創建「MenuView」類(可以擴展View,RelativeLayout或其他),其中定義了所有菜單的圖標及其onClickListeners。然後,這個類的佈局。在佈局中,我建立了兩個linearlayout,它們分別位於屏幕的邊框(這些是指菜單)和一個LinearLayout,其中列內有空白區域,其他活動必須進行。

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類中這樣做。 我膨脹的佈局。

MainClass.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 

回答

0

你不能膨脹Activity。你可以膨脹View和派生類型。所以,你的班級應該延伸ViewViewGroup或某些派生類。

編輯:

Regardins您的充氣例外,你在你的自定義視圖的線55得到一個NPE,可能是因爲,R.id.navButton沒有佈局內聲明的一個嘗試膨脹。

+0

你說要更改爲'公共類MenuViewActivity擴展視圖',但然後在'公共類MainClass延伸活動' – masmic

+0

仍然無法正常工作 – masmic

+0

什麼是不工作的意思? – Blackbelt