我會明白的。Android:動態加載不同佈局的選擇到另一個佈局
我有這個類在它自己的文件,DynamicMapLoader.java:
public class DynamicMapLoader extends Activity{
public void Load(int mapInt) {
int mapArr[] = {
R.id.map1,
R.id.map2
//TODO:expand
};
int mapToLoad = mapArr[mapInt];
FrameLayout flContainer = (FrameLayout) findViewById(R.id.frameLayout3);
LinearLayout flContent = (LinearLayout) findViewById(mapToLoad);
flContainer.addView(flContent);
}
}
我有這樣的代碼,調用DynamicMapLoader在主要活動:
DynamicMapLoader ml = new DynamicMapLoader();
ml.Load(0);
frameLayout3是在main.xml中:
<FrameLayout
android:layout_width="wrap_content"
android:id="@+id/frameLayout3"
android:layout_below="@+id/editText1"
android:layout_alignParentLeft="true"
android:layout_height="350dp">
</FrameLayout>
並且map1是其自己的.xml中的父LinearLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="@+id/scrollView1"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:id="@+id/imageView1"
android:src="@drawable/map_1_road"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<ImageView
android:id="@+id/imageView2"
android:src="@drawable/map_1_road"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
,當我在我的AVD啓動它,logcat中顯示在主活動中FATAL EXCEPTION: main
,並指出ml.Load(0);
,如下圖所示:
10-01 06:20:36.891: ERROR/AndroidRuntime(992): FATAL EXCEPTION: main
10-01 06:20:36.891: ERROR/AndroidRuntime(992): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cep/com.cep.MapricotActivity}: java.lang.NullPointerException
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at android.os.Looper.loop(Looper.java:123)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at java.lang.reflect.Method.invoke(Method.java:507)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at dalvik.system.NativeStart.main(Native Method)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): Caused by: java.lang.NullPointerException
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at android.app.Activity.findViewById(Activity.java:1647)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at com.cep.DynamicMapLoader.Load(DynamicMapLoader.java:31)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at com.cep.MapricotActivity.onCreate(MapricotActivity.java:207)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-01 06:20:36.891: ERROR/AndroidRuntime(992): ... 11 more
有什麼建議?提前致謝。的
我認爲你沒有得到R.id.map1/2在maptoload中的值,而是在兩個獨立的int變量中獲取單獨的值,並將它們分配給mapArr ......這將會工作,我希望如此。 – Hanry
@hanry我做了'int map1 = R.id.map1; \t \t int map2 = R.id.map2;'並分配給每個數組。仍然沒有工作,同樣的錯誤:(無論如何。仍然在尋找一個解決方案。 – mike666234
[this](http://stackoverflow.com/questions/3334048/android-layout-replacing-a-view-with-another-view -on-run-time/3760027#3760027)可能是你的答案 – Hanry