0

我一直在嘗試從廚房中選取圖像並將其設置爲ImageView。我成功地去了畫廊和挑選圖像,但無法將其設置爲ImageView。實際上,我不能在我寫的Fragment類中寫入OnActivityResult。以下是該課程的代碼。無法在片段類中寫入onActivityResult函數

public class MainActivity extends FragmentActivity { 

// create object of FragmentPagerAdapter 
SectionsPagerAdapter mSectionsPagerAdapter; 
String imgDecodableString; 
ImageView callerPic;// = (ImageView) findViewById(R.id.callerPic); 
// viewpager to display pages 
ViewPager mViewPager; 
final static int cameraData = 0; 
private static int RESULT_LOAD_IMG = 1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.slideview_fragment); 

    // Create the adapter that will return a fragment for each of the five 
    // primary sections of the app. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(
      getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

} 

/** 
* A FragmentPagerAdapter that returns a fragment corresponding to one of 
* the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a DummySectionFragment (defined as a static inner class 
     // below) with the page number as its lone argument. 

     switch (position + 1) { 
      case 1: 

       Fragment fragment1 = new Fragment1(); 
       return fragment1; 
      case 2: 

       Fragment fragment2 = new Fragment2(); 
       return fragment2; 


      default: 
       Fragment fragment = new Fragment1(); 
       return fragment; 
     } 
    } 

    @Override 
    public int getCount() { 
     // Show 5 total pages. 
     return 2; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return "Fake CALL"; 
      case 1: 
       return "Fake SMS"; 

     } 
     return null; 
    } 
} 



public static class Fragment1 extends Fragment { 


    public Fragment1() { 
    } 

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


     View view = inflater.inflate(R.layout.activity_main, container, false); 
//THIS PART OF THE CODE I AM HAVING TROUBLE WITH 
     ImageView callerPic = (ImageView) view.findViewById(R.id.callerPic); 
     callerPic.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       // Start the Intent 
       startActivityForResult(galleryIntent, RESULT_LOAD_IMG); 

      } 
     }); 

     return view; 

    } 
} 

public static class Fragment2 extends Fragment { 

    public Fragment2() { 
    } 

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

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

     return view; 
    } 
} 
} 

我必須在Fragment1類中寫入onActivityResult方法,但我不能在裏面寫入。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     // When an Image is picked 
     if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK 
       && null != data) { 
      // Get the Image from data 

      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      // Get the cursor 
      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      // Move to first row 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      imgDecodableString = cursor.getString(columnIndex); 
      cursor.close(); 

      // Set the Image in ImageView after decoding the String 
      callerPic.setImageBitmap(BitmapFactory 
        .decodeFile(imgDecodableString)); 

     } else { 
      Toast.makeText(this, "You haven't picked Image", 
        Toast.LENGTH_LONG).show(); 
     } 
    } catch (Exception e) { 
     Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) 
       .show(); 
    } 

} 

任何人都可以幫助解決這個問題嗎? 謝謝。

+0

'我無法在Fragment類中寫入OnActivityResult ..爲什麼?請添加更多細節。我的應用程序在片段上有onActivityResult並且工作正常。 – Psypher

回答

0

在碎片和活動中單獨保留您的視圖而不是「交叉使用」它們通常是一種很好的模式。

由於NilayDani評論上面,你可以這樣做:

getActivity().startActivityForResult(galleryIntent, RESULT_LOAD_IMG); 

而且.onActivityResult可以在活動中使用。

但要小心,當您使用片段視圖,即ImageView callerPic來確保此視圖可用並且片段已創建且處於活動狀態。如果你不關注Activity-Fragment的生命週期,這很容易導致崩潰。

建議儘量保持您的callerPic僅在Fragment中進行交互,當然如果可能的話。