2016-03-07 68 views
1

我有一個fragment捕獲4個圖像,我想通過他們activity,但我不斷收到null對象。我不知道該怎麼做。從片段到活動傳遞圖像 - gettin空對象

這是我的片段:

public class RegFragFour extends Fragment { 

    Uri imageCaptureUri; 
    ImageView imgCbOne, imgCbTwo, imgCbThree, imgCbFour; 
    Button btnCbChooseOne, btnCbChooseTwo, btnCbChooseThree, btnCbChooseFour, btnCbContinueToFour; 
    static final int PICK_FROM_CAMERA = 1; 
    static final int PICK_FROM_FILE = 2; 
    int pressedButton = 0; 
    String passedPathOne, passedPathTwo, passedPathThree, passedPathFour; 
    onDataPassFour dataPasser; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.reg_four, container, false); 

     imgCbOne = (ImageView) view.findViewById(R.id.imgOne); 
     imgCbTwo = (ImageView) view.findViewById(R.id.imgTwo); 
     imgCbThree = (ImageView) view.findViewById(R.id.imgThree); 
     imgCbFour = (ImageView) view.findViewById(R.id.imgFour); 
     btnCbChooseOne = (Button) view.findViewById(R.id.btnChooseOne); 
     btnCbChooseTwo = (Button) view.findViewById(R.id.btnChooseTwo); 
     btnCbChooseThree = (Button) view.findViewById(R.id.btnChooseThree); 
     btnCbChooseFour = (Button) view.findViewById(R.id.btnChooseFour); 
     btnCbContinueToFour = (Button) view.findViewById(R.id.btnContinueToFour); 

     btnCbChooseTwo.setEnabled(false); 
     btnCbChooseThree.setEnabled(false); 
     btnCbChooseFour.setEnabled(false); 

     final String[] items = new String[]{"From Camera", "From Phone Memory"}; 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_item, items); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle("Select Image"); 
     builder.setAdapter(adapter, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       if (which == 0) { 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        File file = new File(Environment.getExternalStorageDirectory(), "tmp_avatar" + String.valueOf(System.currentTimeMillis()) + ".jpg"); 
        imageCaptureUri = Uri.fromFile(file); 
        try { 
         intent.putExtra(MediaStore.EXTRA_OUTPUT, imageCaptureUri); 
         intent.putExtra("return data", true); 
         startActivityForResult(intent, PICK_FROM_CAMERA); 
        } catch (Exception ex) { 
         ex.printStackTrace(); 
        } 
        dialog.cancel(); 
       } else { 
        Intent intent = new Intent(); 
        intent.setType("image/*"); 
        intent.setAction(Intent.ACTION_GET_CONTENT); 
        startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE); 
       } 
      } 
     }); 
     final AlertDialog dialog = builder.create(); 

     btnCbChooseOne.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pressedButton = 1; 
       dialog.show(); 
      } 
     }); 
     btnCbChooseTwo.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pressedButton = 2; 
       dialog.show(); 
      } 
     }); 
     btnCbChooseThree.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pressedButton = 3; 
       dialog.show(); 
      } 
     }); 
     btnCbChooseFour.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pressedButton = 4; 
       dialog.show(); 
      } 
     }); 
     btnCbContinueToFour.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent Login = new Intent(getActivity(), FakeWelcome.class); 
       startActivity(Login); 
      } 
     }); 
     return view; 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode != Activity.RESULT_OK) 
      return; 
     Bitmap bitmap = null; 
     String path = ""; 
     if (requestCode == PICK_FROM_FILE) { 
      imageCaptureUri = data.getData(); 
      path = getRealPathFromURI(imageCaptureUri); 
      if (path == null) 
       path = imageCaptureUri.getPath(); 
      if (path != null) 
       bitmap = BitmapFactory.decodeFile(path); 
     } else { 
      path = imageCaptureUri.getPath(); 
      bitmap = BitmapFactory.decodeFile(path); 
     } 
     switch (pressedButton){ 
      case 1: 
       imgCbOne.setImageBitmap(bitmap); 
       btnCbChooseTwo.setEnabled(true); 
       passedPathOne = imageCaptureUri.getPath();; 
       break; 
      case 2: 
       imgCbTwo.setImageBitmap(bitmap); 
       btnCbChooseThree.setEnabled(true); 
       passedPathTwo = imageCaptureUri.getPath();; 
       break; 
      case 3: 
       imgCbThree.setImageBitmap(bitmap); 
       btnCbChooseFour.setEnabled(true); 
       passedPathThree = imageCaptureUri.getPath();; 
       break; 
      case 4: 
       imgCbFour.setImageBitmap(bitmap); 
       passedPathFour = imageCaptureUri.getPath();; 
       break; 
     } 
     dataPasser.dataInfoRegFour(passedPathOne, passedPathTwo, passedPathThree, passedPathFour); 
    } 

    public String getRealPathFromURI(Uri contentUri) { 
     String res = null; 
     String[] proj = {MediaStore.Images.Media.DATA}; 
     Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null); 
     if (cursor.moveToFirst()) { 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      res = cursor.getString(column_index); 
     } 
     cursor.close(); 
     return res; 
    } 
    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     try { 
      dataPasser = (onDataPassFour) context; 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public interface onDataPassFour { 
     void dataInfoRegFour(String imgPathOne, String imgPathTwo, String imgPathThree, String imgPathFour); 
    } 
} 

這是我的活動:

public class FakeWelcome extends AppCompatActivity implements RegFragFour.onDataPassFour { 
    ImageView imgCbOne, imgCbTwo, imgCbThree, imgCbFour; 
    Button btnCbChooseOne, btnChooseTwo, btnChooseThree, btnChooseFour; 
    String _imgPathOne, _imgPathTwo, _imgPathThree, _imgPathFour; 
    Bitmap bitmap = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fake_welcome); 
     imgCbOne = (ImageView) findViewById(R.id.imgOne); 
     imgCbTwo = (ImageView) findViewById(R.id.imgTwo); 
     imgCbThree = (ImageView) findViewById(R.id.imgThree); 
     imgCbFour = (ImageView) findViewById(R.id.imgFour); 
     btnCbChooseOne = (Button) findViewById(R.id.btnChooseOne); 
     btnChooseTwo = (Button) findViewById(R.id.btnChooseTwo); 
     btnChooseThree = (Button) findViewById(R.id.btnChooseThree); 
     btnChooseFour = (Button) findViewById(R.id.btnChooseFour); 

     btnCbChooseOne.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       bitmap = BitmapFactory.decodeFile(_imgPathOne); 
       imgCbOne.setImageBitmap(bitmap); 
      } 
     }); 
     btnChooseTwo.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       bitmap = BitmapFactory.decodeFile(_imgPathTwo); 
       imgCbTwo.setImageBitmap(bitmap); 
      } 
     }); 
     btnChooseThree.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       bitmap = BitmapFactory.decodeFile(_imgPathThree); 
       imgCbThree.setImageBitmap(bitmap); 
      } 
     }); 
     btnChooseFour.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       bitmap = BitmapFactory.decodeFile(_imgPathFour); 
       imgCbFour.setImageBitmap(bitmap); 
      } 
     }); 

    } 

    @Override 
    public void dataInfoRegFour(String imgPathOne, String imgPathTwo, String imgPathThree, String imgPathFour) { 
     _imgPathOne = imgPathOne; 
     _imgPathTwo = imgPathTwo; 
     _imgPathThree = imgPathThree; 
     _imgPathFour = imgPathFour; 
    } 
} 

這是我的錯誤:

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.shaka.myparselogin.RegistrationFragments.RegFragFour$onDataPassFour.dataInfoRegFour(android.graphics.Bitmap, android.graphics.Bitmap, android.graphics.Bitmap, android.graphics.Bitmap)' on a null object reference 
                at com.example.shaka.myparselogin.RegistrationFragments.RegFragFour.onActivityResult(RegFragFour.java:168) 
                at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156) 

它指向這一行:

dataPasser.dataInfoRegFour(passedPathOne, passedPathTwo, passedPathThree, passedPathFour); 

有什麼想法?

回答

2

好吧,顯然dataPasser對象爲null。在顯示的代碼片段中,只給出了FragmentsonAttach()方法中的值。因此,您可以在onAttach()中放置一個斷點,調試您的應用程序並確認它實際上從未被調用過。

至於爲什麼它沒有被調用:你似乎正在使用某個版本的AppCompat庫。有例如到onAttach(Context)在計算器上這兩個討論覆蓋的事實onAttach()方法簽名已經從onAttach(Activity)改變:

onAttach() not called in Fragment

Android Fragment onAttach() deprecated

所以大概的問題是,你已經實現了一個需要Context爲一個參數,並且你的Activity試圖調用將Activity作爲參數的那個。

您可以查看例如這兩個鏈接,可能你只需要相應地更改你的方法簽名。

更改代碼:

從...

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    try { 
     dataPasser = (onDataPassFour) context; 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

...到...

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try { 
     dataPasser = (onDataPassFour) activity; 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 
+0

我是新到Android所以它很難,我該怎麼辦你說。你可以試着幫我編碼嗎? –

+0

我添加了代碼更改。 –

+0

我試過了,它仍然不起作用。 –

相關問題