Create a view by Java code
I used the following code to add a view in a layout
LinearLayout linearLayout_row1_recent_released = (LinearLayout)findViewById(R.id.linearLayout);
View myview = findViewById(R.layout.row);
Button button = myview.findViewById(R.id.mybutton);
linearLayout_row1_recent_released.removeAllViews();
linearLayout_row1_recent_released.addView(myview,LinearLayout.LayoutParams.FILL_PARENT);
Using row.xml i've tried to get a view. What i get is a FATAL EXCEPTION.
02-11 02:59:04.278: ERROR/AndroidRuntime(780): FATAL EXCEPTION: main
02-11 02:59:04.278: ERROR/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaa.ap/com.aaaaaa.ap.AppPlaceMain}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaaa.ap/com.aaaaaa.ap.Store}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaa.ap/com.aaaaaa.ap.catalog.Home}: java.lang.NullPointerException
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1717)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:984)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:126)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:3900)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:491)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaaa.ap/com.aaaaaa.ap.Store}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaaaa.ap/com.aaaaaaa.ap.catalog.Home}: java.lang.NullPointerException
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1543)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:130)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:342)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:680)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.widget.TabHost.setCurrentTab(TabHost.java:349)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1665)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): ... 11 more
02-11 02:59:04.278: ERROR/AndroidRuntime(780): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaaaa.ap/com.aaaaaa.ap.catalog.Home}: java.lang.NullPointerException
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1543)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:130)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:342)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at com.aaaaad.ap.Store.onCreate开发者_如何转开发(Store.java:29)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1665)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): ... 20 more
02-11 02:59:04.278: ERROR/AndroidRuntime(780): Caused by: java.lang.NullPointerException
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.view.ViewGroup.addView(ViewGroup.java:2684)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at com.aaaaa.ap.catalog.Home.onCreate(Home.java:51)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1665)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): ... 26 more
Is there any other good way to add views dynamically to a linear layout? What have I done wrong here ?
I am guessing that the view R.layout.row is defined in a separate XML file, and you want to include it here. If that is the case,
findViewById(R.layout.row)
will not work because R.layout.row is not visible in the current activity.
You need to use layout inflater to inflate the view, and then you can add it to the linear layout. The way to do this is
LayoutInflaterinflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.row, null);
linearLayout_row1_recent_released.addView(myview,
new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT) );
Your call to addView
is wrong.
You are calling addView(View, int), but it should be: addView(View, LayoutParams):
linearLayout_row1_recent_released.addView(myview,
new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT) );
精彩评论