我有一个带有 FrameLayout 的 tabhost 视图,一旦在我的 java 中选择了一个选项卡,我就会创建新的意图并在 FrameLayout 中调用不同的活动。
现在我正在尝试从父活动访问子活动(FrameLayout 内的那个)视图,但它不起作用。这是示例代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_download_activity);
tabHost = getTabHost();
addTab("Download File", R.drawable.ic_action_download,DownloadActivity.class);
}
private void addTab(String labelId, int drawable, Class<?> c) {
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec(labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.custom_tabs, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawable);
icon.setScaleType(ImageView.ScaleType.FIT_CENTER);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
现在名为 DownloadActivity 的活动有它自己的 xml 布局,我想从这个活动中访问它
错误日志
02-15 06:08:09.147: E/AndroidRuntime(2588): FATAL EXCEPTION: main
02-15 06:08:09.147: E/AndroidRuntime(2588): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.utext.unext/com.utext.unext.BookFrame}: java.lang.NullPointerException
02-15 06:08:09.147: E/AndroidRuntime(2588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-15 06:08:09.147: E/AndroidRuntime(2588): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-15 06:08:09.147: E/AndroidRuntime(2588): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-15 06:08:09.147: E/AndroidRuntime(2588): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-15 06:08:09.147: E/AndroidRuntime(2588): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 06:08:09.147: E/AndroidRuntime(2588): at android.os.Looper.loop(Looper.java:137)
02-15 06:08:09.147: E/AndroidRuntime(2588): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-15 06:08:09.147: E/AndroidRuntime(2588): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 06:08:09.147: E/AndroidRuntime(2588): at java.lang.reflect.Method.invoke(Method.java:511)
02-15 06:08:09.147: E/AndroidRuntime(2588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-15 06:08:09.147: E/AndroidRuntime(2588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-15 06:08:09.147: E/AndroidRuntime(2588): at dalvik.system.NativeStart.main(Native Method)
02-15 06:08:09.147: E/AndroidRuntime(2588): Caused by: java.lang.NullPointerException
02-15 06:08:09.147: E/AndroidRuntime(2588): at com.utext.unext.BookFrame.createView(BookFrame.java:190)
02-15 06:08:09.147: E/AndroidRuntime(2588): at com.utext.unext.BookFrame.onCreate(BookFrame.java:62)
02-15 06:08:09.147: E/AndroidRuntime(2588): at android.app.Activity.performCreate(Activity.java:5104)
02-15 06:08:09.147: E/AndroidRuntime(2588): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-15 06:08:09.147: E/AndroidRuntime(2588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-15 06:08:09.147: E/AndroidRuntime(2588): ... 11 more
希望有意义