我有两个片段ScanFragment.java和ProductViewFragment.java我想将字符串pid的值从ScanFragment传递给ProductViewFragment。但是,我得到了java.lang.NullPointerException。
1)ScanFragment.java
PreviewCallback previewCb = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = scanner.scanImage(barcode);
if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
scanText.setText(/**"barcode result " + **/sym.getData());
barcodeScanned = true;
}
String pid = ((TextView) getView().findViewById(R.id.scanText)).getText()
.toString();
Bundle bundle = new Bundle();
bundle.putString("pid", pid);
ProductViewFragment fragment = new ProductViewFragment();
fragment.setArguments(bundle);
FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.replace(R.id.frame_container, new ProductViewFragment(), "Product view");
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.commit();
}
}
};
2)ProductViewFragment.java
public class ProductViewFragment extends Fragment {
public ProductViewFragment(){}
TextView txtName;
TextView txtPrice;
TextView txtDesc;
TextView productErrorMsg;
EditText editTextQuantity;
ImageView image;
Button btnAddtocart;
Button btnCancel;
public static String pid;
// JSON parser class
JSONParser1 jsonParser = new JSONParser1();
// single product url
private static final String url_product_details = "http://www.zulanawi.com/learn2crack_login_api/";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCT = "product";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_PRICE = "price";
private static final String TAG_DESCRIPTION = "description";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_product, container, false);
// Loader image - will be shown before loading image
int loader = R.drawable.loader;
// save button
btnAddtocart = (Button) rootView.findViewById(R.id.btnAddtocart);
btnCancel = (Button) rootView.findViewById(R.id.btnCancel);
productErrorMsg = (TextView) rootView.findViewById(R.id.product_error);
// Imageview to show
image = (ImageView) rootView.findViewById(R.id.image);
// getting product details from intent
//Intent i = getIntent();
// getting product id (pid) from intent
//pid = getActivity().getIntent().getStringExtra(TAG_PID);
//Bundle bundle = this.getArguments();
pid = getArguments().getString("pid");
// Image url
String image_url = "http://xxx.xxx.com/upload/products/" + pid +".jpg";
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getActivity().getApplicationContext());
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage(image_url, loader, image);
// Getting complete product details in background thread
//new NetCheck().execute();
new GetProductDetails().execute();
// Add to cart button click event
btnAddtocart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// starting background task to update product
//new SaveProductDetails().execute();
//Get Global Controller Class object (see application tag in AndroidManifest.xml)
//Product arraylist size
//Intent addToCart = new Intent(getActivity().getApplicationContext(), ScreenFirstFragment.class);
//startActivity(addToCart);
FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.replace(R.id.frame_container, new ScreenFirstFragment(), "First Screen");
mFragmentTransaction.commit();
}
});
3)
The log. 03-31 15:46:36.230: E/AndroidRuntime(6971): at info.androidhive.slidingmenu.ProductViewFragment.onCreateView(ProductViewFragment.java:99) is pid = getArguments().getString("pid"); at ProductViewFragment
03-31 15:45:57.735: E/(6971): Device driver API match
03-31 15:45:57.735: E/(6971): Device driver API version: 17
03-31 15:45:57.735: E/(6971): User space API version: 17
03-31 15:45:57.755: E/(6971): mali: REVISION=Linux-r3p1-01rel1 BUILD_DATE=Tue Jul 2 15:06:24 KST 2013
03-31 15:46:36.230: E/AndroidRuntime(6971): FATAL EXCEPTION: main
03-31 15:46:36.230: E/AndroidRuntime(6971): java.lang.NullPointerException
03-31 15:46:36.230: E/AndroidRuntime(6971): at info.androidhive.slidingmenu.ProductViewFragment.onCreateView(ProductViewFragment.java:99)
03-31 15:46:36.230: E/AndroidRuntime(6971): at android.app.Fragment.performCreateView(Fragment.java:1695)
03-31 15:46:36.230: E/AndroidRuntime(6971): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
03-31 15:46:36.230: E/AndroidRuntime(6971): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
03-31 15:46:36.230: E/AndroidRuntime(6971): at android.app.BackStackRecord.run(BackStackRecord.java:682)
03-31 15:46:36.230: E/AndroidRuntime(6971): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
03-31 15:46:36.230: E/AndroidRuntime(6971): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
03-31 15:46:36.230: E/AndroidRuntime(6971): at android.os.Handler.handleCallback(Handler.java:725)
03-31 15:46:36.230: E/AndroidRuntime(6971): at android.os.Handler.dispatchMessage(Handler.java:92)
03-31 15:46:36.230: E/AndroidRuntime(6971): at android.os.Looper.loop(Looper.java:137)
03-31 15:46:36.230: E/AndroidRuntime(6971): at android.app.ActivityThread.main(ActivityThread.java:5279)
03-31 15:46:36.230: E/AndroidRuntime(6971): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 15:46:36.230: E/AndroidRuntime(6971): at java.lang.reflect.Method.invoke(Method.java:511)
03-31 15:46:36.230: E/AndroidRuntime(6971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-31 15:46:36.230: E/AndroidRuntime(6971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-31 15:46:36.230: E/AndroidRuntime(6971): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
使用
mFragmentTransaction.replace(R.id.frame_container,fragment, "Product view");
通过ProductViewFragment
传递您呼叫setArguments
的{{1}}的相同实例,而不是新实例。