我试图在android studio上为我的项目制作应用程序,但是当我试图运行这个java文件时,这个文件崩溃了,应用程序停止在我的模拟器上运行。我只是想尝试将图像从我的默认图库导入我的应用程序。
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 :(得分:0)
请查看android的开发者部分,特别是关于如何使用片段及其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。