1

我试图使用下面的程序创建一个矩形。但它抛出运行时异常。模拟器正在打开但意外关闭。在我的项目中,我有两个类 -Map.java 和 StartDraw.java

以下是 Map.java 的代码:

 package com.mapping;

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

public class Map extends View {
Paint paint = new Paint();

public Map(Context context) {
    super(context);            
}

@Override
public void onDraw(Canvas canvas) {
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);
    canvas.drawRect(30, 30, 80, 80, paint);
    paint.setStrokeWidth(0);
    paint.setColor(Color.CYAN);
    canvas.drawRect(33, 60, 77, 77, paint );
    paint.setColor(Color.YELLOW);
    canvas.drawRect(33, 33, 77, 60, paint );

}

下面是 StartDraw.java 的代码

package com.mapping;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class StartDraw extends Activity {
Map drawView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    drawView = new Map(this);
    drawView.setBackgroundColor(Color.WHITE);
    setContentView(drawView);

}
}

下面是 manifest.xml

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

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

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

请帮我解决这个异常。我怀疑这可能是因为构造函数,但无法得到修改什么以及如何修改。

下面是logcat

04-03 11:23:56.611: D/dalvikvm(333): newInstance failed: no <init>()
04-03 11:23:56.632: D/AndroidRuntime(333): Shutting down VM
04-03 11:23:56.632: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-03 11:23:56.701: E/AndroidRuntime(333): FATAL EXCEPTION: main
04-03 11:23:56.701: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mapping/com.mapping.Map}: java.lang.InstantiationException: com.mapping.Map
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-03 11:23:56.701: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 11:23:56.701: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
04-03 11:23:56.701: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 11:23:56.701: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 11:23:56.701: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)
04-03 11:23:56.701: E/AndroidRuntime(333): Caused by: java.lang.InstantiationException: com.mapping.Map
04-03 11:23:56.701: E/AndroidRuntime(333):  at java.lang.Class.newInstanceImpl(Native Method)
04-03 11:23:56.701: E/AndroidRuntime(333):  at java.lang.Class.newInstance(Class.java:1409)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-03 11:23:56.701: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
04-03 11:23:56.701: E/AndroidRuntime(333):  ... 11 more
04-03 11:24:04.161: I/Process(333): Sending signal. PID: 333 SIG: 9
4

3 回答 3

1

您在地图中提供自定义视图名称,<activity>您需要为活动名称而不是视图名称设置名称“StartDraw”

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

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

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>
于 2013-04-03T06:22:03.163 回答
0

注册StartDraw而不是默认注册Map ,因为您正在扩展 Activity 类。所以只需在清单中将默认活动名称更改为:AndroidManifest.xmlActivityStartDrawStartDraw

<activity
        android:name=".StartDraw" <!--here use StartDraw instead Map  -->
        android:label="@string/app_name" >
<!-- other attributes here -->
</activity>
于 2013-04-03T06:22:16.737 回答
0

只是改变androidmanifest.xml而不是android:name="com.mapping.Map"

<activity android:name=".startdraw" android:label="@string/app_name" android:launchMode="singleTask">
于 2013-04-03T06:41:23.293 回答