开发者

Android icon error

I copied this code from a tutorial site because I am trying to learn it. But I am getting an error at

p开发者_StackOverflow社区ackage com.android.test;
import android.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
public class Rotate extends Activity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         LinearLayout linearLayout = new LinearLayout(this);

   Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

On this last line, I get an error where ".icon" is written. It says, "icon cannot be resolved or is not a field."

int width = bitmap.getWidth(); int height = bitmap.getHeight();

   Matrix matrix = new Matrix();
   matrix.postRotate(90);

   Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0,

width, height, matrix, true); BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);

   ImageView imageView = new ImageView(this);
   imageView.setImageDrawable(bmd);
   imageView.setScaleType(ScaleType.CENTER);

   linearLayout.addView(imageView, new LinearLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
   setContentView(linearLayout);
     }
 }


In your import statements, you have

import android.R;

That means it'll look in for android.R.drawable.icon, rathern than R.drawable.icon. android.R contains the IDs to all the assets from the SDK. To access your own assets, you need to remove the using statement, or manually write com.your.package.name.R.drawable.icon


Icon some times works and some times it does not. Change it to 'ic_launcher'. This works all the time. This is the file name found under 'res/drawable-?dpi'. It is the icon that appears on the android device.

Like this: .setIcon(R.drawable.ic_launcher)


This is very likely because you don't have a image in your /res/drawable folder called icon.png/icon.bmp/icon.jpg

So, copy the image probably on the site in to that folder and it should be fine.


You have to copy an image called icon.png into the res/drawable directory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜