开发者

addView crashes on android 2.2, works fine on 2.1 and 1.6

im using addVIew, addview is called using frameLayout, and my Ball Class is passed as a argument which extends View, and it uses onDraw function

addView is called inside a button listener. program loads fine but crashes whenever i press the button.

everything works in android 1.6 and 2.1 emulator, and 2.1 HTC phone. it crashes using android 2.2 emulator and 2.2 HTC phone

if i comment the main.addview statement, everything works fine even in 2.2

logcat report attached aswell

Ball class

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 * 开发者_Python百科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, from pressing RUN on eclipse, till force quit on android emulator ERROR occurs when i press the button

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}


Looks like you are passing an invalid Context... not sure why type-checking's not catching it, since it looks like you're passing a ViewParent, which shouldn't compile.

Try changing the first parameter to your Ball instantiation to this:

new Ball(tyre.this,...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜