我有一个带有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
希望有意义
答案 0 :(得分:0)
我有类似的问题。这是我的代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Removes the title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Intent i = getIntent();
mExtras = i.getExtras();
mScreenNum = mExtras.getString("num");
setContentView(R.layout.tab);
int a = Integer.parseInt(mScreenNum);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
title = (TextView) findViewById(R.id.title_money2);
// Removes the default keyboard
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
TabHost tabHost = getTabHost();
// Creating tab here
ImageView iv_tabl_selected = new ImageView(this);
iv_tabl_selected.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
iv_tabl_selected.setImageResource(R.drawable.tab_indicator_bill);
ImageView iv_tab2_selected = new ImageView(this);
iv_tab2_selected.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
iv_tab2_selected.setImageResource(R.drawable.tab_indicator_recharge);
ImageView iv_tab3_selected = new ImageView(this);
iv_tab3_selected.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
iv_tab3_selected.setImageResource(R.drawable.tab_indicator_dth);
// Displaying the Tab host which are created
TabSpec tab_first= tabHost.newTabSpec("One");
tab_first.setIndicator(iv_tabl_selected);
TabSpec tab_second= tabHost.newTabSpec("two");
tab_second.setIndicator(iv_tab2_selected);
TabSpec tab_three = tabHost.newTabSpec("three");
tab_three.setIndicator(iv_tab3_selected);
// Switching between Tabs
mActivity1 = new Intent(this, Activity1.class);
mActivity2= new Intent(this, Activity2.class);
mActivity3= new Intent(this, Activity3.class);
tab_first.setContent(mActivity1 );
tab_second.setContent(mActivity2);
tab_three.setContent(mActivity3);
// The order in which the tabs are displayed
tabHost.addTab(tab_first);
tabHost.addTab(tab_second);
tabHost.addTab(ttab_three);
//tabHost.getTabWidget().getChildTabViewAt(2).setOnClickListener(null);
// Switch case to select Tab from Home screen
switch (a) {
case TAB1:
tabHost.setCurrentTab(0);
tab_recharge.setIndicator(iv_tabl_selected);
break;
case TAB2:
tabHost.setCurrentTab(1);
tab_dth.setIndicator(iv_tab2_selected);
break;
case TAB3:
tabHost.setCurrentTab(2);
tab_billpayment.setIndicator(iv_tab3_selected);
break;
}
}
});
}
}