我收到以下崩溃报告
Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/3322 (has extras) }} to activity {com.example.instareport/com.example.instareport.MainActivity}: java.lang.NullPointerException
我的fragment_main.xml看起来像这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.instareport.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="visible"
android:id="@+id/imageView1" />
</RelativeLayout>
我的MainActivity类位于
之下public class MainActivity extends Activity {
static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView mImageView;
private Bitmap imageBitmap;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
super.onActivityResult(requestCode, resultCode, data);
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView)findViewById(R.id.imageView1);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
dispatchTakePictureIntent();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
基本上,当应用程序启动时,它应该调用相机应用程序,因为它有一个意图,当我按下快门按钮时,我按下确定(接受图像),然后它开始崩溃。
我尝试在下一行插入断点,这就是代码抛出异常的地方
mImageView.setImageBitmap(imageBitmap);
为什么会这样?我尝试从官方Android网站和Android教程中关注Android教程。
由于
编辑。
这里是logcat
07-17 21:16:12.131: W/dalvikvm(21693): threadid=1: thread exiting with uncaught exception (group=0x41522438)
07-17 21:16:12.191: E/AndroidRuntime(21693): FATAL EXCEPTION: main
07-17 21:16:12.191: E/AndroidRuntime(21693): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/3322 (has extras) }} to activity {com.example.instareport/com.example.instareport.MainActivity}: java.lang.NullPointerException
07-17 21:16:12.191: E/AndroidRuntime(21693): at android.app.ActivityThread.deliverResults(ActivityThread.java:3205)
07-17 21:16:12.191: E/AndroidRuntime(21693): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3248)
07-17 21:16:12.191: E/AndroidRuntime(21693): at android.app.ActivityThread.access$1100(ActivityThread.java:138)
07-17 21:16:12.191: E/AndroidRuntime(21693): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
07-17 21:16:12.191: E/AndroidRuntime(21693): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 21:16:12.191: E/AndroidRuntime(21693): at android.os.Looper.loop(Looper.java:137)
07-17 21:16:12.191: E/AndroidRuntime(21693): at android.app.ActivityThread.main(ActivityThread.java:4911)
07-17 21:16:12.191: E/AndroidRuntime(21693): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 21:16:12.191: E/AndroidRuntime(21693): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 21:16:12.191: E/AndroidRuntime(21693): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-17 21:16:12.191: E/AndroidRuntime(21693): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-17 21:16:12.191: E/AndroidRuntime(21693): at dalvik.system.NativeStart.main(Native Method)
07-17 21:16:12.191: E/AndroidRuntime(21693): Caused by: java.lang.NullPointerException
07-17 21:16:12.191: E/AndroidRuntime(21693): at com.example.instareport.MainActivity.onActivityResult(MainActivity.java:66)
07-17 21:16:12.191: E/AndroidRuntime(21693): at android.app.Activity.dispatchActivityResult(Activity.java:5492)
07-17 21:16:12.191: E/AndroidRuntime(21693): at android.app.ActivityThread.deliverResults(ActivityThread.java:3201)
07-17 21:16:12.191: E/AndroidRuntime(21693): ... 11 more
答案 0 :(得分:1)
public class MainActivity extends Activity {
static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView mImageView;
private Bitmap imageBitmap;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
super.onActivityResult(requestCode, resultCode, data);
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
// imageBitmap = (Bitmap) data.getExtras().get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView1);
dispatchTakePictureIntent();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
删除fragment_main.xml。并编辑activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.instareport.MainActivity"
tools:ignore="MergeRootFrame" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
如果您检索到的位图是可以接受的,那么就这样做吧。如果它的尺寸太小,您可以在http://pastebin.com/NF9yu0sa
获得更好的解决方案答案 1 :(得分:0)
您是否已确定其数据或附加内容是否为空? (如果没有,请快速检查)。 你还试过没有&#34; super&#34;线?
抱歉,我还不能发表评论。
答案 2 :(得分:0)
NullPointer异常的原因是您没有初始化位图变量。您的代码可能是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView)findViewById(R.id.imageView1);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.imageView1);
****Rest of your code ********
}
答案 3 :(得分:-2)
引起:java.lang.NullPointerException 07-17 21:16:12.191:E / AndroidRuntime(21693):at com.example.instareport.MainActivity.onActivityResult(MainActivity.java:66)
看看这个。 Nullpointer会阻止您的Activity启动。 您的代码中的第66行是哪一行? 可能 extras.get(“data”)为Bitmap返回null。