0
嗨,有人能告訴我什麼是錯誤的下面的代碼。 logcat在onCreate方法中表示它。所有的主要活動似乎都是正確的,因爲我已經通過了字符串包。Android Java,getString從主要活動到一個片段問題
@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();
如果您的應用程序崩潰,請從logcat發佈堆棧跟蹤 – Karakuri
檢查您的包是否爲空。並且在顯示片段時檢查你是否使用了setArguments()。 – joao2fast4u
bundle不爲null,使用setArguments的iam。我將發佈日誌 – user3247335