gpt4 book ai didi

android - 在 xml 中声明时自定义 View 不起作用

转载 作者:行者123 更新时间:2023-11-30 04:41:23 24 4
gpt4 key购买 nike

我有一个应用程序,它的 Activity 有一个扩展 View 的内部类。 View 类显示位图,一切正常。我现在要做的是在显示位图的 View 上放置一个按钮。为此,我将原始内部类 TouchView 设为一个单独的类。然后我在相对布局内和按钮下方的布局文件夹中的 xml 文件中声明它。我认为这会让按钮出现在 View 顶部。我收到以下错误。有谁知道为什么?谢谢

05-09 11:45:22.603: ERROR/AndroidRuntime(12211): Uncaught handler: thread main exiting due to uncaught exception
05-09 11:45:22.613: ERROR/AndroidRuntime(12211): java.lang.NullPointerException
05-09 11:45:22.613: ERROR/AndroidRuntime(12211): at android.graphics.Canvas.throwIfRecycled(Canvas.java:954)
05-09 11:45:22.613: ERROR/AndroidRuntime(12211): at android.graphics.Canvas.drawBitmap(Canvas.java:980)
05-09 11:45:22.613: ERROR/AndroidRuntime(12211): at com.tecmark.TouchView.onDraw(TouchView.java:172)
05-09 11:45:22.613: ERROR/AndroidRuntime(12211): at android.view.View.draw(View.java:6535)

代码:

public class Jjilapp extends Activity {



private static final String TAG = "*********jjil";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "***********inside oncreate about to set contentview = ");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.touchview);


}


}

在此输入验证码

class TouchView extends View{


private File tempFile;
private byte[] imageArray;
private Bitmap bgr;
private Bitmap bm;
private Bitmap whitebm;
private Paint pTouch;
private int centreX = 1;
private int centreY = 1;
private int radius = 50;





public TouchView(Context context, AttributeSet attr) {
super(context,attr);
}

public TouchView(Context context) {
super(context);

.........code that gets bitmap and changes it





}// end of touchView constructor


public void changePixel(){


for (int i=0; i < bgr.getWidth(); ++i) {
for (int y=0; y < bgr.getHeight(); ++y) {

if( Math.sqrt( Math.pow(i - centreX, 2) + ( Math.pow(y - centreY, 2) ) ) <= radius ){

bgr.setPixel(i,y,Color.rgb(255,255,255));
}
}
}

}// end of changePixel()


@Override
public boolean onTouchEvent(MotionEvent ev) {

......code for touch events
}//end of onTouchEvent


@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);

canvas.drawBitmap(whitebm, 0, 0, null);
canvas.drawBitmap(bgr, 0, 0, null);
canvas.drawCircle(centreX, centreY, radius,pTouch);


}//end of onDraw


}

布局:

<?xml version="1.0" encoding="utf-8" ?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content" android:gravity="fill">


<Button android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>

<com.tecmark.TouchView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />


</RelativeLayout>

最佳答案

将你的两个构造函数都改成这个

public TouchView(Context context) {
super(context);
TouchView(context, null);
}

public TouchView(Context context, AttributeSet attr) {
super(context,attr);

.........code that gets bitmap and changes it

}// end of touchView constructor

把你的xml改成这个

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill">

<com.tecmark.TouchView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
</RelativeLayout>

关于android - 在 xml 中声明时自定义 View 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5936145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com