2013-02-15 75 views
0

我有一個帶有FrameLayout的tabhost視圖,一旦在我的java內部選擇了一個選項卡,我會創建新的intent並調用FrameLayout中的不同活動。Android Access TabHost父視圖中的視圖

現在我試圖從父活動訪問子活動(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

我有類似的問題。這裏是我的代碼

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


     } 
    }); 

} 

}

+0

這僅僅是標籤添加到佈局,並呼籲他們的活動我所尋找的是標籤訪問活動的XML佈局 – 2013-02-15 06:43:40