gpt4 book ai didi

android - addView 在 android 2.2 上崩溃,在 2.1 和 1.6 上工作正常

转载 作者:行者123 更新时间:2023-12-02 16:46:17 24 4
gpt4 key购买 nike

我使用addVIew,使用frameLayout调用addview,我的Ball类作为扩展View的参数传递,并且它使用onDraw函数

addView 在按钮监听器内调用。程序加载正常,但每当我按下按钮时就会崩溃。

在 android 1.6 和 2.1 模拟器以及 2.1 HTC 手机中一切正常。使用 android 2.2 模拟器和 2.2 HTC 手机会崩溃

如果我评论 main.addview 语句,即使在 2.2 中一切也正常

还附上 logcat 报告

球类

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

public class Ball extends View{
private final float left;
private final float right;
private final float top;
private final float bottom;
private final float scale;
private float startAngle;
private float sweepAngle;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

public Ball(Context context, float a, float b, float c, float d, Paint e, float f, float g, float scale) {

super(context);
this.mPaint = e;
this.left = a;
this.top = b;
this.right = c;
this.bottom = d;
this.startAngle = f;
this.sweepAngle = g;
this.scale = scale;
}

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

RectF clockRect = new RectF(left, top, right, bottom);
canvas.drawArc(clockRect, startAngle, sweepAngle, true, this.mPaint);
canvas.drawLine(160*scale, 120*scale, 160*scale, 260*scale, this.mPaint);
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>



<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">


<RelativeLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:id="@+id/RelativeLayout01"
>
<Button android:text="Calculate"
android:layout_height="45dip"
android:layout_width="90dip"
android:id="@+id/Button01"
android:layout_below="@+id/Spinner06"
android:layout_alignRight="@+id/Spinner06">
</Button>
</RelativeLayout>
</FrameLayout>

轮胎.java

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class tyre extends Activity {
/** Called when the activity is first created. */

FrameLayout main1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

main1 = (FrameLayout) findViewById(R.id.main_view);

final Button button1 = (Button) findViewById(R.id.Button01);

final Paint mPaint3= new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint3.setColor(0xFFFFFFFF);

button1.setOnClickListener
(
new View.OnClickListener()
{
public void onClick(View v)
{
float clear = (float) (0.75*22*2.54 + (0.7*2*365 * 80 / 1000));
final float scale = getBaseContext().getResources().getDisplayMetrics().density;
float centerX = 160;
float centerY = 190;

main1.addView( new Ball(getParent(),(centerX-clear)*scale,(centerY-clear)*scale,(centerX+clear)*scale,(centerY+clear)*scale,mPaint3,0,360,scale));

}
}
);
}
}

catlog,从在 eclipse 上按 RUN,直到在 android 模拟器上强制退出当我按下按钮时发生错误

01-14 12:39:17.567: INFO/ActivityManager(59): Start proc com.aimenrg.tyrecalc for activity com.aimenrg.tyrecalc/.tyre: pid=965 uid=10033 gids={}
01-14 12:39:18.797: INFO/ActivityManager(59): Displayed activity com.aimenrg.tyrecalc/.tyre: 1365 ms (total 1365 ms)
01-14 12:39:24.387: DEBUG/dalvikvm(127): GC_EXPLICIT freed 765 objects / 43384 bytes in 163ms
01-14 12:39:29.476: DEBUG/dalvikvm(157): GC_EXPLICIT freed 156 objects / 11336 bytes in 201ms
01-14 12:39:34.427: DEBUG/dalvikvm(268): GC_EXPLICIT freed 28 objects / 1400 bytes in 144ms
01-14 12:39:47.447: DEBUG/AndroidRuntime(965): Shutting down VM
01-14 12:39:47.447: WARN/dalvikvm(965): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): FATAL EXCEPTION: main
01-14 12:39:47.467: ERROR/AndroidRuntime(965): java.lang.NullPointerException
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at android.view.ViewConfiguration.get(ViewConfiguration.java:211)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at android.view.View.<init>(View.java:1814)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at com.aimenrg.tyrecalc.Ball.<init>(Ball.java:21)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at com.aimenrg.tyrecalc.tyre$1.onClick(tyre.java:153)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at android.view.View.performClick(View.java:2408)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at android.view.View$PerformClick.run(View.java:8816)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at android.os.Handler.handleCallback(Handler.java:587)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at android.os.Looper.loop(Looper.java:123)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at java.lang.reflect.Method.invoke(Method.java:521)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-14 12:39:47.467: ERROR/AndroidRuntime(965): at dalvik.system.NativeStart.main(Native Method)
01-14 12:39:47.489: WARN/ActivityManager(59): Force finishing activity com.aimenrg.tyrecalc/.tyre
01-14 12:39:48.017: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{43f49210 com.aimenrg.tyrecalc/.tyre}
01-14 12:39:51.377: INFO/Process(965): Sending signal. PID: 965 SIG: 9
01-14 12:39:51.412: INFO/ActivityManager(59): Process com.aimenrg.tyrecalc (pid 965) has died.
01-14 12:39:51.417: INFO/WindowManager(59): WIN DEATH: Window{43f98158 com.aimenrg.tyrecalc/com.aimenrg.tyrecalc.tyre paused=false}
01-14 12:39:51.537: WARN/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 965 uid 10033
01-14 12:39:58.478: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{43f49210 com.aimenrg.tyrecalc/.tyre}

最佳答案

看起来你正在传递一个无效的上下文...不知道为什么类型检查没有捕获它,因为它看起来像你正在传递一个 ViewParent,它不应该编译。

尝试将 Ball 实例化的第一个参数更改为:

new Ball(tyre.this,...

关于android - addView 在 android 2.2 上崩溃,在 2.1 和 1.6 上工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4690271/

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