Android Java,从主要活动到碎片问题的getString

时间:2014-03-30 19:57:52

标签: java android

您可以告诉我以下代码的错误吗? logcat在onCreate方法中说明了它。所有这些在mainactivity中都是正确的,因为我已经通过字符串传递了包。

@SuppressLint("NewApi")
public class CurrentBookingFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {

    View view =inflater.inflate(R.layout.current_booking_fragment, container, false);

    Bundle bundle = this.getArguments();

    String strtext = this.getArguments().getString("GetStringPUFirstLine");

    TextView CurrentBookingFragmentTV = (TextView)view.findViewById(R.id.CurrentBookingFragmentTV);

    CurrentBookingFragmentTV.setText(strtext);

    return view;
    }
 }

这是日志:

03-30 21:59:30.385: E/AndroidRuntime(30843):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at dalvik.system.NativeStart.main(Native Method)
03-30 21:59:30.385: E/AndroidRuntime(30843): Caused by: android.view.InflateException: Binary XML file line #45: Error inflating class fragment
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.app.Activity.setContentView(Activity.java:1956)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at com.example.prototype.MainActivity.onCreate(MainActivity.java:88)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.app.Activity.performCreate(Activity.java:5372)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
03-30 21:59:30.385: E/AndroidRuntime(30843):    ... 11 more
03-30 21:59:30.385: E/AndroidRuntime(30843): Caused by: java.lang.NullPointerException
03-30 21:59:30.385: E/AndroidRuntime(30843):    at com.example.prototype.CurrentBookingFragment.onCreateView(CurrentBookingFragment.java:49)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.app.Fragment.performCreateView(Fragment.java:1699)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:879)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1155)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.app.Activity.onCreateView(Activity.java:4966)
03-30 21:59:30.385: E/AndroidRuntime(30843):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
03-30 21:59:30.385: E/AndroidRuntime(30843):    ... 21 more

创建片段:

     Fragment CurrentBookingFragment;
     CurrentBookingFragment = new CurrentBookingFragment();
     FragmentManager fm = getFragmentManager();

     Bundle bundle=new Bundle();
     bundle.putString("GetStringPUFirstLine", "GetStringPUFirstLine");

     CurrentBookingFragment.setArguments(bundle);

     FragmentTransaction fragmentTransaction = fm.beginTransaction();
     fragmentTransaction.replace(R.id.BookingFragment, CurrentBookingFragment);
     fragmentTransaction.commit();

1 个答案:

答案 0 :(得分:0)

尝试在onActivityCreated(){}中设置文本。

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    TextView CurrentBookingFragmentTV = (TextView)view.findViewById(R.id.CurrentBookingFragmentTV);

    CurrentBookingFragmentTV.setText(strtext);    
}