这是我的MainActivity.java 我使用//标记为不在某些行上使用,并发现该行以int t =开头 是导致应用程序崩溃并强制关闭的问题。
package com.example.camera_test;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 1337;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
@SuppressLint("NewApi") @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, CAMERA_PIC_REQUEST );
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@SuppressLint("NewApi") protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
int t = thumbnail.getByteCount();
TextView age = (TextView) findViewById(R.id.textView1);
age.setText(Integer.toString(t));
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
}
我点击它时添加了一个按钮,它会在我拍摄照片后打开相机,它会显示我在一个小窗口拍摄的照片。它正在发挥作用。
然后我添加了这3行:
int t = thumbnail.getByteCount();
TextView age = (TextView) findViewById(R.id.textView1);
age.setText(Integer.toString(t));
我想在我的设备屏幕上显示图像的ByteCount。
我添加一行后:int t = thumbnail.getByteCount();有一个错误,所以我做了自动修复,它添加了@SuppressLint(“NewApi”)
然后我标记了//这3行,一旦我取消标记并使用了行int t = thumbnail.getByteCount();所以拍完照片之后就崩溃了,告诉我需要强行关闭。
为什么它会在这条线上崩溃?
这是文件activity_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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="45dp"
android:layout_marginTop="62dp"
android:text="Activate The Camera" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
我无法弄清楚为什么它会与int t行崩溃。
我在int t行添加了一个断点,但它永远不会停在那里,当我添加一个断点为什么时,我的应用程序上的任何地方都不会停止?
答案 0 :(得分:2)
getByteCount()可从API级别12(android 3.1+)获得。 您确定要构建/运行此版本还是以上版本?
另外,请在功能中添加LogCat,更容易找到错误。