コードの何が問題なのかわかりません..いくつかの異なることを試しましたが、何も機能しません..独自のonDrawを実行する必要があるため、ビューを拡張するクラスと、そのカスタムビューが配置されるxmlがあります. わかりましたので、ここにコードがあります..
mainimagehandler.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<net.stojakovic.example.ImageMainHandler.Panel
android:id="@+id/custom_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
カスタムビューを使用したアクティビティ
package net.stojakovic.example;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.actionbarsherlock.app.SherlockActivity;
public class ImageMainHandler extends SherlockActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainimagehandler);
}
public static class Panel extends View {
public Panel(Context context) {
super(context);
Log.w("Panel","constructor");
}
public Panel(Context context, AttributeSet attrs){
super(context, attrs);
}
@Override
public void onDraw(Canvas canvas) {
Log.w("Panel","onDraw");
}
}
}
このようにすると、LogCat メッセージでアプリがクラッシュします。
java.lang.RuntimeException: アクティビティ ComponentInfo を開始できません {net.stojakovic.example/net.stojakovic.example.ImageMainHandler}: android.view.InflateException: バイナリ XML ファイルの行 #8: クラス net.stojakovic.example.ImageMainHandler の膨張エラー。パネル
このように xml でカスタム ビューを定義すると、アプリはクラッシュしませんが、何も起こりません。コンストラクターは呼び出されません。
<View
class="net.stojakovic.example.ImageMainHandler.Panel"
android:id="@+id/custom_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>