Android:使用选项卡在不同片段之间切换
Posted
技术标签:
【中文标题】Android:使用选项卡在不同片段之间切换【英文标题】:Android: Switching between different fragments using tabs 【发布时间】:2015-02-23 09:02:42 【问题描述】:我正在尝试实现工具栏选项卡,并让每个选项卡膨胀不同的片段,但是我遇到了错误:
java.lang.RuntimeException: Unable to start activity ComponentInfocom.danielkim.soundrecorder/com.danielkim.soundrecorder.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
这是我在MainActivity.java
中的适配器:
public class MyAdapter extends FragmentPagerAdapter
private String[] titles = "Record", "Saved Recordings" ;
public MyAdapter(FragmentManager fm)
super(fm);
@Override
public Fragment getItem(int position)
switch(position)
case 0:
return RecordFragment.newInstance(position);
case 1:
return FileViewerFragment.newInstance(position);
return null;
@Override
public int getCount()
return titles.length;
@Override
public CharSequence getPageTitle(int position)
return titles[position];
和RecordFragment.java
:
public static RecordFragment newInstance(int position)
RecordFragment f = new RecordFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View recordView = inflater.inflate(R.layout.fragment_record, container, false);
mChronometer = (Chronometer) recordView.findViewById(R.id.chronometer);
mRecordButton = (FloatingActionButton) recordView.findViewById(R.id.btnRecord);
mRecordButton.setColorNormal(getResources().getColor(R.color.accent));
mRecordButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
onRecord(mStartRecording);
mStartRecording = !mStartRecording;
);
mPauseButton = (ImageButton) recordView.findViewById(R.id.btnPause);
mPauseButton.setVisibility(View.GONE); //hide pause button before recording starts
return recordView;
任何帮助将不胜感激,谢谢!
编辑:
这是完整的堆栈跟踪。我将适配器更改为使用 return RecordFragment.newInstance(position)
,但仍然看到相同的错误。
12-25 16:16:42.845 2889-2889/com.danielkim.soundrecorder E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.danielkim.soundrecorder, PID: 2889
java.lang.RuntimeException: Unable to start activity ComponentInfocom.danielkim.soundrecorder/com.danielkim.soundrecorder.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
at com.danielkim.soundrecorder.fragments.RecordFragment.onCreate(RecordFragment.java:76)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5949)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
【问题讨论】:
【参考方案1】:getArguments()
正在返回 null(因此,getArguments().getInt(ARG_POSITION)
的 NPE)因为您在创建 Fragment 后没有调用 setArguments()
。
通常的模式是在 Fragment 类中有一个工厂方法来预加载参数。在这种情况下,它应该或多或少类似于:
public static RecordFragment newInstance(int position)
RecordFragment f = new RecordFragment();
Bundle args = new Bundle();
args.putInt(ARG_POSITION, position);
f.setArguments(args);
return f;
然后使用该方法创建Fragment,而不是直接调用构造函数。
【讨论】:
我也尝试过这种方法,但它似乎也抛出了同样的错误:public static RecordFragment newInstance(int position) RecordFragment f = new RecordFragment(); Bundle b = new Bundle(); b.putInt(ARG_POSITION, position); f.setArguments(b); return f;
在适配器中使用return RecordFragment.newInstance(position);
(抱歉,格式很糟糕,不知道如何解决这个问题! )
@DanielKim 可能还有其他错误。请发布完整的堆栈跟踪。
嗨,我已经编辑了完整的堆栈跟踪(使用return RecordFragment.newInstance(position)
),谢谢!
@DanielKim 将position = getArguments().getInt(ARG_POSITION);
移动到onCreateView()
。【参考方案2】:
你可以使用
onCreateView()
方法在每个片段中并使用
ViewHolders
和
LayoutInflater
你可以生成任何你想要的视图...
【讨论】:
以上是关于Android:使用选项卡在不同片段之间切换的主要内容,如果未能解决你的问题,请参考以下文章