我正在开发一个具有多个片段的应用程序,我正在使用 ViewpageIndicator 库和 Sherlocakactionbar 库。在片段之一中,我必须有一个按钮,它将调用意图作为 startactivityforResult 从相机获取图像。但我得到的错误是 savefragmentBasicState。我曾在 Activity 工作过并且知道这一点,但我仍然不明白这个片段是如何工作的,人们正在这样做,所以让我更加困惑。我应该如何拯救国家?
添加监控.java
public class Addmonitoring extends SherlockFragmentActivity {
private static final String[] CONTENT = new String[] { "Info",
"Budget", "Accessibility", "Engagement", "Effictiveness",
"Additional Info","Documents","Picture","Video","Finalize" };
private FragmentPagerAdapter adapter;
private ViewPager pager;
public static final String PREFS_NAME = "MyPrefsFile";
private static final int[] ICONS = new int[] {
R.drawable.ic_info, /* info */
R.drawable.ic_budget, /* budget */
R.drawable.ic_accessibility, /* accessibility */
R.drawable.ic_engagement, /* Engagement */
R.drawable.ic_effectiveness, /* Effectiveness */
R.drawable.ic_additionalinfo, /* Additional info */
R.drawable.ic_documents, /* Documents */
R.drawable.ic_photos, /* Pictures */
R.drawable.ic_video, /* Video */
R.drawable.ic_addmonitoring_info /* Save */
};
String projectid;
ProgressDialog progressDialog;
dbhelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addmonitoring);
Bundle bundle = getIntent().getExtras();
adapter = new StarterMenuAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
pager.setCurrentItem(0);
}
class StarterMenuAdapter extends FragmentPagerAdapter implements
IconPagerAdapter {
public StarterMenuAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragmant = null;
switch (position) {
case 0:
fragmant = new Fragment1_info();
break;
case 1:
fragmant = new Fragment2_budget();
break;
case 2:
fragmant = new Fragment3_accessibility();
break;
case 3:
fragmant = new Fragment4_Engagement();
break;
case 4:
fragmant = new Fragment5_Effectiveness();
break;
case 5:
fragmant = new Fragment6_AdditionalInfo();
break;
case 6:
fragmant = new Fragment7_Document();
break;
case 7:
fragmant = new Fragment8_Pictures();
break;
case 8:
fragmant = new Fragment9_Video();
break;
case 9:
fragmant = new Fragment10_Save();
break;
default:
break;
}
return fragmant;
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length];
}
@Override
public int getIconResId(int index) {
return ICONS[index];
}
@Override
public int getCount() {
return CONTENT.length;
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
Fragment8_Pictures.java
public class Fragment8_Pictures extends SherlockFragment implements
OnClickListener {
Uri capturedImageUri = null;
ImageView image1, image2, image3, image4, image5, image6;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.addmonitoring8_picture,
container, false);
image1 = (ImageView) view.findViewById(R.id.mon_image1);
image2 = (ImageView) view.findViewById(R.id.mon_image2);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mon_image1:
Calendar cal = Calendar.getInstance();
File file = new File(Environment.getExternalStorageDirectory(),
("devcheck_monitoring" + cal.getTimeInMillis() + ".jpg"));
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
capturedImageUri = Uri.fromFile(file);
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
getActivity().startActivityForResult(i, 301);
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 301) {
// Bitmap photo = (Bitmap) data.getExtras().get("data");
// imageView.setImageBitmap(photo);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(),
capturedImageUri);
image1.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Logcat 错误
08-20 10:58:57.223: E/AndroidRuntime(6131): FATAL EXCEPTION: main
08-20 10:58:57.223: E/AndroidRuntime(6131): java.lang.NullPointerException
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
08-20 10:58:57.223: E/AndroidRuntime(6131): at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:127)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.app.Activity.performSaveInstanceState(Activity.java:1152)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1216)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3257)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3316)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.app.ActivityThread.access$900(ActivityThread.java:153)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1274)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.os.Looper.loop(Looper.java:137)
08-20 10:58:57.223: E/AndroidRuntime(6131): at android.app.ActivityThread.main(ActivityThread.java:5232)
08-20 10:58:57.223: E/AndroidRuntime(6131): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 10:58:57.223: E/AndroidRuntime(6131): at java.lang.reflect.Method.invoke(Method.java:511)
08-20 10:58:57.223: E/AndroidRuntime(6131): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
08-20 10:58:57.223: E/AndroidRuntime(6131): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
08-20 10:58:57.223: E/AndroidRuntime(6131): at dalvik.system.NativeStart.main(Native Method)