嗨,我正在使用实现接口的Fragments.
public class SigninFragment extends Fragment implements SigninInterface 
片段类中接口的方法实现如下.
@Override
public void afterSubmitClicked(String userId, Bundle bundle) {
    Log.d(TAG,"Calling time afterSubmitClicked called"+bundle);
    if(!userId.equals("-1")){
        //Logged in successfully
        //Move to MusicHome
        Intent mIntent = new Intent(getActivity(),MusicHome.class);
        mIntent.putExtra("SigninFragment.user_details", bundle);
        startActivity(mIntent);
    }else{
        //Logging in failed
        //show error dialog
    }
}
在执行AsynchronousTask(扩展AsyncTask)类之后调用此方法.
但是我崩溃了.并显示错误消息
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
logcat的
   02-14 16:37:04.648: E/AndroidRuntime(28177): Process: com.raaga.android, PID: 28177
02-14 16:37:04.648: E/AndroidRuntime(28177): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on …我有一个活动,我用片段替换了.该活动采用了一个Intent,它有一些关于活动应该显示的数据的额外信息.
既然我的Activity只是一个包含同样功能的Fragment的包装器,那么如果我用XML标记带有标签的XML片段,我如何将该包带到Fragment?
如果我使用FragmentTransaction将Fragment放入ViewGroup,我将有机会在Fragment构造函数中传递此信息,但我想知道片段是用XML定义的情况.
我正在尝试实现具有"共享元素"的片段之间的转换,如新材料设计规范中所述.我能找到的唯一方法是ActivityOptionsCompat.makeSceneTransitionAnimation,我认为它仅适用于Activity.我一直在寻找相同的功能,但有/片段.
我在容器Activity上使用此方法来显示BFrag
public void showBFrag()
{
    // Start a new FragmentTransaction
    FragmentTransaction fragmentTransaction = mFragmentMgr.beginTransaction();
    if(mBFrag.isAdded())
    {
        Log.d(LOG_TAG, "Show() BFrag");
        fragmentTransaction.show(mBFrag);   
    }
    else
    {
        Log.d(LOG_TAG, "Replacing AFrag -> BFrag");
        fragmentTransaction.replace(R.id.operation_fragments_frame, mBFrag);
    }
    // Keep the transaction in the back stack so it will be reversed when backbutton is pressed
    fragmentTransaction.addToBackStack(null);
    // Commit transaction
    fragmentTransaction.commit();        
}
我从容器Activity中调用它; 首次:
然后我按下后退按钮:
然后我通过从同一个Activity调用showBFrag()再次前进:
所以:
编辑:这是异常的完整信息.
06-07 12:08:32.730: ERROR/AndroidRuntime(8576): java.lang.IllegalStateException: Fragment …android fragment illegalstateexception android-fragments android-3.0-honeycomb
我想在我的应用程序中显示警告对话框.我正在使用片段.我尝试了以下代码来执行此操作:
 AlertDialog ad = new AlertDialog.Builder(context)
            .create();
    ad.setCancelable(false);
    ad.setTitle(title);
    ad.setMessage(message);
    ad.setButton(context.getString(R.string.ok_text), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
ad.show();
但它崩溃了,logcat中的错误是:
04-18 15:23:01.770:E/AndroidRuntime(9424):android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序
从互联网上我开始知道崩溃是由于上下文问题.我给了上下文
context = this.getActivity().getApplicationContext();
我不知道这有什么问题.有谁能够帮我?
我有一组标签FragmentActivity,每个标签都有自己的片段.当我尝试通过一个onClickListener和使用该startActivity(myIntent)方法从该片段中启动一个新活动时,我的应用程序强制关闭.
环顾四周后,我找到了一个或两个被称为方法的参考startActivityFromFragment,但在搜索了大约一个小时之后,我找不到任何解释或如何使用它的例子,或者这是我应该使用的.
我想我要问的是,从活动启动新活动和从片段启动新活动之间是否有任何区别,如果是,我需要实现什么?
一直在寻找这个问题但现在无济于事:
如何确定片段是从backstack恢复?我在FragmentActivity中使用兼容性库和ListFragment.选择ListFragment中的项目时,将启动新的片段以替换ListFragment.
我注意到当FragmentActivity暂停时,会调用Fragment的onSaveInstanceState.但是当Fragment通过FragmentTransaction放入后端堆栈时,onSaveInstanceState不会被调用,那么使用null savedInstanceState Bundle调用生命周期方法onCreateView和onActivityCreated.
我问这个是因为我想在创建或恢复Fragment时加载一些数据,但是当用户通过它回来时却不是这样.堆栈中.
我看过如何检查片段是否从后台恢复? 但希望增加更多细节,希望这会引发答案.
但请注意:可以在onDestroy()之前的任何时间调用此方法.在许多情况下,片段可能大部分被拆除(例如放置在没有显示UI的后台堆栈上),但是在其拥有的活动实际上需要保存其状态之前,其状态将不会被保存.
所以onSaveInstanceState绝对是不可能的......
android acl fragment android-compatibility android-support-library
片段和自定义视图可以实现类似的功能,我知道片段比自定义视图更具可重用性,使用片段的任何其他好处/增强功能?片段是否应该取代自定义视图,或者只是针对某些特定目的的增强?
例如,下面的代码是片段:
public class TestFragment extends Fragment {
    private TextView tv_name;
    private Button btn_play;
    private Button btn_delete;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.testfragment, container, false);
    }
    @Override
    public void onStart() {
        super.onStart();
        tv_name = (TextView)getView().findViewById(R.id.tv_name);
        btn_play = (Button)getView().findViewById(R.id.btn_play);
        btn_delete = (Button)getView().findViewById(R.id.btn_delete);
    }
}
自定义视图的代码:
public class TestCustomView extends LinearLayout {
    private TextView tv_name;
    private Button btn_play;
    private Button btn_delete;
    public TestCustomView(Context context, AttributeSet attrs){
        super(context, attrs);
        setOrientation(LinearLayout.HORIZONTAL);
        setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        tv_name …我有一个ActionBarActivity片段.我正在使用FragmentPagerAdapter它为我的应用程序提供片段.我的问题如何在Fragment中访问父Activity视图?
在我的活动中:
public class tabsmain extends Activity{
    public static Context appContext;
    public boolean lf_ch=false;
    public void onCreate(Bundle savedInstanceState){
我想访问并可能从tabsmain中的片段更改lf_ch;
public class tabquests extends Fragment{ 
    public CheckBox lc;
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)//onCreateView
    { 
lc.setChecked(//set it to lf_ch);
但是,我似乎无法访问lf_ch的值.