开发者

Android: dynamically loading different layouts of choice into another layout

开发者 https://www.devze.com 2023-04-09 13:16 出处:网络
I\'ll get to the point. I have this class in its own file, DynamicMapLoader.java: public class DynamicMapLoader extends Activity{

I'll get to the point.

I have this class in its own file, 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) find开发者_高级运维ViewById(mapToLoad);
    flContainer.addView(flContent);
}
}

I have this code, calling DynamicMapLoader in the main activity:

DynamicMapLoader ml = new DynamicMapLoader();
ml.Load(0);

frameLayout3 is in the 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>

and map1 is the parent LinearLayout in its own .xml:

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

when i start it in my AVD, LogCat shows a FATAL EXCEPTION: main and points to ml.Load(0); in the main activity, as shown below:

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

Any suggestions? Thanks in advance.


You have to setContentView(R.Layout. the layout file ) before calling findViewByID() Or inflate the layout to a view and then view.findViewByID() to get the desired view


instead of

int mapArray[]...

try

Integer mapArray[]...

Also please post the full output under FATAL EXCEPTION: main so that we may see what went wrong at a more detailed level

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号