2017-05-24 49 views
0

我已經創建了一個Android應用程序到我的最終項目 它有一個選項卡活動與三個選項卡,在每個選項卡中有很多組件。例如,在第二個選項卡中,我有一個WebView和一個按鈕,當我點擊按鈕時,webview必須加載一些數據。
我該如何處理代碼?
在tab1activity.java是不可能的,因爲mainactivity.java中也沒有findViewById()這使我的應用程序崩潰 我該怎麼辦?如何訪問標籤活動中的視圖?

+1

請將您的代碼發佈 –

回答

0

你必須讓你的按鈕和webview在你的tabTwo佈局。而且你也必須爲你的TabTwoFragment創建一個類。將此代碼放到TabTwoFragment類中以訪問其視圖:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 

View rootView = inflater.inflate(R.layout.tab_two_layout, parent, false); 
WebView webView = (WebView) rootView.findViewById(R.id.your_web_view); 
Button button = (Button) rootView.findViewById(R.id.your_button); 

// Set Your clicklistener on your button here. 

return rootView; 
}