Android 什么';使用int变量有什么问题?使应用程序崩溃

Android 什么';使用int变量有什么问题?使应用程序崩溃,android,eclipse,Android,Eclipse,这是我的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.Int

这是我的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));
我还想在我的设备上显示屏幕上图像的字节数

添加行后:int t=thumboil.getByteCount();有一个错误,所以我做了自动修复,它添加了@SuppressLint(“NewApi”)

然后,在取消标记并使用int t=thumboil.getByteCount()行时,我用//标记了这3行;所以在我拍了一张照片后,它坠毁了,并告诉我它需要强行关闭

为什么它会在这条线上崩溃

这是文件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行上添加了一个断点,但当我添加断点时,它从未停止在那里,也从未停止在应用程序的任何位置。为什么

  • getByteCount()可从API级别12(android 3.1+)获得。 您确定正在生成/运行此版本或更高版本吗


    另外,请在功能中添加LogCat,这样更容易找到错误。

    Nicklas现在我将int t行更改为:ByteArrayOutputStream bao=new ByteArrayOutputStream();缩略图.压缩(Bitmap.CompressFormat.PNG,100,bao);字节[]ba=bao.toByteArray();int size=ba.length;TextView年龄=(TextView)findViewById(R.id.textView1);age.setText(整数.toString(大小));它正在向我展示它。但是我该如何改变呢?我的意思是,在Eclipse中将API级别更改为12或更高?如果我做了更改,这个应用程序会在更低的api级别上运行吗?我的意思是低于Android 3.1?我看到当我运行这个应用程序时,目标是2.2如果你使用更高的api功能,那么它不会在更低的版本上运行。但是一些更高的API函数是可以使用的,因为它们在较低的版本中被忽略,与清单属性(如installLocation等)类似。如果您的其他解决方案适用于较低版本,则如果您希望支持这些版本,则应使用它。您还可以检查设备在运行时运行的版本,并确定是否应使用计算字节的“新”或“旧”版本。类似于if(Build.VERSION.SDK_INT