0
我試圖開發具有TabHost/TabWidget標籤視圖爲Android 4.2.1(目標是三星Galaxy Tab 2)API級16NPE在則tabspec,從片段處理TabHost/TabWidget
由於TabActivity已棄用,我找不到容易的樣本。
在應用程序終止與NPE:
07-30 15:30:04.617: E/AndroidRuntime(18526): FATAL EXCEPTION: main
07-30 15:30:04.617: E/AndroidRuntime(18526): java.lang.NullPointerException
07-30 15:30:04.617: E/AndroidRuntime(18526): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:640)
07-30 15:30:04.617: E/AndroidRuntime(18526): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:635)
07-30 15:30:04.617: E/AndroidRuntime(18526): at android.widget.TabHost$TabSpec.setContent(TabHost.java:484)
07-30 15:30:04.617: E/AndroidRuntime(18526): at fr.lfinance.crm.ProspectDetailFragment.onCreateView(ProspectDetailFragment.java:53)
的片段的代碼:
public class ProspectDetailFragment extends Fragment {
[...]
@Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
final View rootView =
inflater.inflate(R.layout.fragment_prospect_detail,container,false);
if(_prospect != null) {
final TabHost tabHost =
(TabHost)rootView.findViewById(R.id.prospect_detail);
final TabSpec tabSpec = tabHost.newTabSpec("rawdatatab");
tabSpec.setIndicator("Données brutes");
tabSpec.setContent(R.id.rawdata); //<<<<<<<<<<<<<<<<<<< HERE IS NPE
tabHost.addTab(tabSpec);
final GridView gvw = (GridView)rootView.findViewById(R.id.rawdata);
final List<String> lst = new ArrayList<String>(2*_properties.size());
for(final String[] pair : _properties) {
lst.add(pair[0]);
lst.add(pair[1]);
}
final ArrayAdapter<String> adapter =
new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, lst);
gvw.setAdapter(adapter);
[... adding 3 others tabs]
}
return rootView;
}
}
文件rawdata.xml
:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:numColumns="2"
android:id="@+id/rawdata"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProspectDetailFragment" />
文件fragment_prospect_detail.xml
:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/prospect_detail"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
文件AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.lfinance.crm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="fr.lfinance.crm.ProspectListActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="fr.lfinance.crm.ProspectDetailActivity"
android:parentActivityName=".ProspectListActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ProspectListActivity" />
</activity>
</application>
</manifest>
我怎樣才能建立一個則tabspec避免這樣的NPE?
您是否覆蓋了以下步驟:http://developer.android.com/guide/topics/ui/layout/tabs.html#example我似乎無法找到此原因,但讓我再檢查一遍。 – g00dy
我認爲你的'Activity'應該擴展'TabActivity'。 **編輯**或這低於棄用? – g00dy
我可以想到的其他東西 - 也粘貼在這裏的清單。 – g00dy