我是Android新手,我正在尝试设置操作栏。 当我运行应用程序时,我得到了一个NPE。
这是我的代码:
public class HomeActivity extends Activity implements TabListener {
private List<Fragment> fragList;
public HomeActivity() {
super();
fragList = new ArrayList<>();
fragList.add(new HomeFragment());
fragList.add(new SecondFragment());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpActionBar();
}
protected void setUpActionBar() {
// Set up the action bar to show a dropdown list.
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the navigation tabs in the action bar.
Tab homeTab = actionBar.newTab();
homeTab.setText("Home");
homeTab.setTabListener(this);
actionBar.addTab(homeTab);
Tab secondTab = actionBar.newTab();
secondTab.setText("Second");
secondTab.setTabListener(this);
actionBar.addTab(secondTab);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
int tabPosition = tab.getPosition();
Fragment f = fragList.get(tabPosition);
ft.replace(android.R.id.content, f);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragList.size() > tab.getPosition()) {
ft.remove(fragList.get(tab.getPosition()));
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
onTabSelected(tab, ft);
}
}
HomeFragment:
public class HomeFragment extends NavigationFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_fragment, container);
return v;
}
}
NavigationFragment,到目前为止无所作为:
public abstract class NavigationFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
和SecondFragment:
public class SecondFragment extends NavigationFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.second_fragment, container);
return v;
}
然后这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lh.mypackage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".HomeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
活动xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lh.mypackage.HomeActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am the home button" />
</FrameLayout>
主片段xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lh.mypackage.HomeActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am the home view" />
</FrameLayout>
第二个片段xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lh.mypackage.HomeActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am the temperature view" />
</FrameLayout>
当我运行应用程序时,这是堆栈跟踪
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lh.mypackage/com.lh.mypackage.HomeActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3739)
at android.view.ViewGroup.addView(ViewGroup.java:3610)
at android.view.ViewGroup.addView(ViewGroup.java:3555)
at android.view.ViewGroup.addView(ViewGroup.java:3531)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:914)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
at android.app.BackStackRecord.run(BackStackRecord.java:682)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
at android.app.Activity.performStart(Activity.java:5381)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
... 11 more
答案 0 :(得分:2)
您可能忘记了setContentView(yourLayoutId)
HomeActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/***
HERE You FORGOT setContentView();
***/
setUpActionBar();
}
答案 1 :(得分:2)
替换行:
View v = inflater.inflate(R.layout.second_fragment, container);
使用:
View v = inflater.inflate(R.layout.second_fragment, container, false);
现在您正在将膨胀的视图添加到容器中。