0

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) findViewById(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.

  • I think you are not getting values of R.id.map1/2 in maptoload instead get individual values in two seperate int variables and assign them to mapArr......THis will work I hope so. – Hanry Oct 01 '11 at 05:32
  • @hanry i did `int map1 = R.id.map1; int map2 = R.id.map2;` and assigned each to the array. still did not work, same error :( thanks anyway. Still looking for a solution. – mike666234 Oct 01 '11 at 05:58
  • [this](http://stackoverflow.com/questions/3334048/android-layout-replacing-a-view-with-another-view-on-run-time/3760027#3760027) could be your answer. – Hanry Oct 01 '11 at 06:14
  • @hanry hmm. thanks! this solved some other problems i had. – mike666234 Oct 01 '11 at 06:19
  • You should not be instantiating your own activities. The operating system will do this for you when you fire an `Intent`. Please read up on the Activity lifecycle (http://developer.android.com/reference/android/app/Activity.html) and how to properly fire intents (http://developer.android.com/guide/topics/fundamentals.html#ActivatingComponents). – Jake Wharton Oct 01 '11 at 06:45

2 Answers2

1

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

weakwire
  • 9,284
  • 8
  • 53
  • 78
  • I've added `setContentView(R.layout.main);` above the FrameLayout object. Now LogCat shows the same NPE error, but pointing at `setContentView(R.layout.main);` – mike666234 Oct 02 '11 at 10:28
  • Yes. although it's now frameLayout1, since I updated and removed unnecessary frameLayouts. But that's besides the point. frameLayout1 is in main.xml, as stated in the original question. – mike666234 Oct 02 '11 at 11:58
0

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

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Isaac
  • 1