指南针app无法实例化活动

时间:2014-04-21 23:47:11

标签: java android view

我正在尝试创建一个基于罗盘的应用程序。我有两个java文件,一个是Compass类,它扩展View,第二个是Sensors类。在与后一类相关的xml文件中,有一个<View>项,如下所示。问题是当我运行应用程序时,它崩溃并且logcat生成以下输出。请帮我解决这个问题。

XML:

     <view 
        android:id="@+id/mycompass" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent" 
        class="com.example.compasstest00.CompassTest00"/>

Compass类的实例化:

CompassTest00 myCompass;
myCompass = (CompassTest00) findViewById(R.id.mycompass);

CompassClass:

public class CompassTest00 extends View {

private float direction;

public CompassTest00(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public CompassTest00(Context context, AttributeSet attrs) {
      super(context, attrs);
      // TODO Auto-generated constructor stub
     }

public CompassTest00(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      // TODO Auto-generated constructor stub
     }

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
      MeasureSpec.getSize(heightMeasureSpec));      
}

 @Override
 protected void onDraw(Canvas canvas) {
      int w = getMeasuredWidth();
      int h = getMeasuredHeight();
      int r;
      if(w > h){
      r = h/2;
       }else{
        r = w/2;
       }
      Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
      paint.setStyle(Paint.Style.STROKE);
      paint.setStrokeWidth(5);
      paint.setColor(Color.WHITE);

      canvas.drawCircle(w/2, h/2, r, paint);

      paint.setColor(Color.RED);
      canvas.drawLine(
        w/2,
        h/2,
        (float)(w/2 + r * Math.sin(-direction)),
        (float)(h/2 - r * Math.cos(-direction)),
        paint);
}

 public void update(float dir){
      direction = dir;
      invalidate();
     }  
}

manifest资源配置文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.compasstest00"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.compasstest00.CompassTest00"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.compasstest00.AndroidSensors"
        android:label="@string/title_activity_android_sensors" >
    </activity>
</application>

LogcatOutput:

04-22 02:50:06.817: D/dalvikvm(8545): newInstance failed: no <init>()
04-22 02:50:06.817: D/AndroidRuntime(8545): Shutting down VM
04-22 02:50:06.817: W/dalvikvm(8545): threadid=1: thread exiting with uncaught  
exception (group=0x419a1700)
04-22 02:50:06.817: E/AndroidRuntime(8545): FATAL EXCEPTION: main
04-22 02:50:06.817: E/AndroidRuntime(8545): java.lang.RuntimeException: Unable to  
instantiate activity   
ComponentInfo{com.example.compasstest00/com.example.compasstest00.CompassTest00}:   
java.lang.InstantiationException: can't instantiate class   
com.example.compasstest00.CompassTest00; no empty constructor
04-22 02:50:06.817: E/AndroidRuntime(8545):     at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2232)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at   
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at  
android.app.ActivityThread.access$700(ActivityThread.java:168)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at   
android.os.Handler.dispatchMessage(Handler.java:99)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at 
android.os.Looper.loop(Looper.java:137)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at 
android.app.ActivityThread.main(ActivityThread.java:5493)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at   
java.lang.reflect.Method.invokeNative(Native Method)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at   
java.lang.reflect.Method.invoke(Method.java:525)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at   
dalvik.system.NativeStart.main(Native Method)
04-22 02:50:06.817: E/AndroidRuntime(8545): Caused by:   
java.lang.InstantiationException: can't instantiate class    
com.example.compasstest00.CompassTest00; no empty constructor
04-22 02:50:06.817: E/AndroidRuntime(8545):     at  
java.lang.Class.newInstanceImpl(Native Method)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at  
java.lang.Class.newInstance(Class.java:1130)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at    
android.app.Instrumentation.newActivity(Instrumentation.java:1078)
04-22 02:50:06.817: E/AndroidRuntime(8545):     at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2223)
04-22 02:50:06.817: E/AndroidRuntime(8545):     ... 11 more

1 个答案:

答案 0 :(得分:0)

试试这个:

<com.example.compasstest00.CompassTest00 
    android:id="@+id/mycompass" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" />

而不是:

<view 
    android:id="@+id/mycompass" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    class="com.example.compasstest00.CompassTest00"/>

<强>更新

事实上,在XML中声明自定义视图的两种方式都应该有效。实际问题出在Android Manifest:

 <activity
    android:name="com.example.compasstest00.CompassTest00"

==&GT;您将自定义视图声明为活动。这就是为什么ActivityThread.performLaunchActivity无法实例化它的原因。 (因为它不应该!)

==&GT;您应该在Android清单中声明一个活动类。然后,此活动可以将customview设置为其contentView。