Caused by: java.lang.IllegalStateExc

Android 码拜 9年前 (2015-04-23) 891次浏览 0个评论
 

代码如下

public class MainActivity extends Activity {
	ImageView iv_bottom,iv_top;
	Bitmap topImage,bottomImage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		iv_bottom=(ImageView) findViewById(R.id.iv_bottom);
		iv_top=(ImageView) findViewById(R.id.iv_top);


		topImage=BitmapFactory.decodeResource(getResources(), R.drawable.a2);
		bottomImage=BitmapFactory.decodeResource(getResources(), R.drawable.b2);

		iv_top.setImageBitmap(topImage);
		iv_bottom.setImageBitmap(bottomImage);


		topImage.setPixel(topImage.getWidth()/2,topImage.getHeight()/2,Color.TRANSPARENT);
		System.out.println(topImage.getWidth()/2+","+topImage.getHeight()/2);
		iv_top.setImageBitmap(topImage);


	}


	@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;
	}

}

错误信息:

04-16 04:26:16.850: E/AndroidRuntime(791): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stripclothes/com.stripclothes.MainActivity}: java.lang.IllegalStateException
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.os.Looper.loop(Looper.java:137)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.app.ActivityThread.main(ActivityThread.java:5103)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at java.lang.reflect.Method.invokeNative(Native Method)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at java.lang.reflect.Method.invoke(Method.java:525)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at dalvik.system.NativeStart.main(Native Method)
04-16 04:26:16.850: E/AndroidRuntime(791): Caused by: java.lang.IllegalStateException
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.graphics.Bitmap.setPixel(Bitmap.java:1227)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at com.stripclothes.MainActivity.init(MainActivity.java:33)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at com.stripclothes.MainActivity.onCreate(MainActivity.java:18)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.app.Activity.performCreate(Activity.java:5133)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-16 04:26:16.850: E/AndroidRuntime(791): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-16 04:26:16.850: E/AndroidRuntime(791): 	... 11 more

运行时显示topImage.setPixel(topImage.getWidth()/2,topImage.getHeight()/2,Color.TRANSPARENT);这附近出错,我仔细看了下应该没错呀    请问我哪块不对了

Caused by: java.lang.IllegalStateExc
20分
因为你的bitmap:topImage是从文件中读取的,直接获得的bitmap是不可变的,不能操作。
你可以把topImage copy一份,然后对copy对象进行改变。
Caused by: java.lang.IllegalStateExc
恩恩   清楚了  

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Caused by: java.lang.IllegalStateExc
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!