即时使用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>
tyre.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,从日食上按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}
答案 0 :(得分:1)
看起来你传递了一个无效的Context ...不确定为什么类型检查没有捕获它,因为它看起来像是在传递一个不应该编译的ViewParent。
尝试将第一个参数更改为Ball实例化:
new Ball(tyre.this,...