2016-01-29 142 views
-1

我正在嘗試爲我的項目在android工作室做一個應用程序,但是當我試圖運行此java文件時,此文件崩潰,應用程序停止工作在我的模擬器上。試圖將圖像從我的默認畫廊導入我的應用程序。在android工作室停止工作的應用程序

import .... 


public class tenant_profile extends Fragment { 

private final int SELECT_PHOTO = 1; 
private ImageView imageView; 
public tenant_profile() { 
    // Required empty public constructor 
} 


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

    // Inflate the layout for this fragment 

    super.onCreateView(inflater, container, savedInstanceState); 

    return inflater.inflate(R.layout.fragment_tenant_profile, container, false); 
} 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 



    Button pickImage = (Button) getView().findViewById(R.id.btn_pick); 
    pickImage.setOnClickListener(new OnClickListener() { 
    //enter code here 
     @Override 
     public void onClick(View view) { 
      Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
      photoPickerIntent.setType("image/*"); 
      startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
     } 
    }); 
} 

    @Override 
    public void onActivityResult (int requestCode, int resultCode, Intent imageReturnedIntent){ 
     super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

     switch (requestCode) { 
      case SELECT_PHOTO: 
       if (resultCode == Activity.RESULT_OK) { 
        try { 
         final Uri imageUri = imageReturnedIntent.getData(); 
         final InputStream imageStream = getContext().getContentResolver().openInputStream(imageUri); 
         final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); 
         imageView.setImageBitmap(selectedImage); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } 


       } 
     } 
    } 



} 

我的異常以下:

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: FATAL EXCEPTION: main 

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: java.lang.NullPointerException 

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:  at com.example.sanchit.helloworld.tenant_profile.onCreate(tenant_profile.java:55) 

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:  at android.support.v4.app.Fragment.performCreate(Fragment.java:1939) 

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:988) 

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:  at android.support.v4.app.FragmentManagerImpl.moveToState 
(FragmentManager.java:1207) 

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572) 

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493) 

請幫助

+0

當*應用程序停止工作*,那麼我相信logcat中還有更多的東西。紅色?我相信'pickImage'在這裏是空的。 – Rohit5k2

+0

@ Rohit5k2我在上面添加了紅色的logcat紅線。請看一看 – logan

回答

0

請看看機器人的開發人員部分尤其是培訓如何使用碎片及其lifecycle

甚至在佈局變得膨脹之前,您嘗試通過ID查找查看。

要解決您的問題,您可以比較關於調用onCreate和onCreateView順序片段的生命週期。

ArticleFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.article_view, container, false); 
     //Get Button here 
     Button pickImage = (Button) view.findViewById(R.id.btn_pick); 
     //Set listener etc. 
     return view; 

    } 
} 

此外,我鼓勵你設置你的pickImage作爲一個成員變量,以便能夠在比onCreateView更多的方法中使用它。