我正在尝试使用 TabHost / TabWidget for Android 4.2.1(目标是三星 Galaxy Tab 2)API 级别 16 开发一个选项卡式视图。
由于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?