我尝试使用以下程序创建一个矩形。但它抛出了运行时异常。模拟器正在打开但意外关闭。在我的项目中,我有两个类-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
答案 0 :(得分: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>
答案 1 :(得分:0)
在StartDraw
中注册Map
而不是AndroidManifest.xml
作为默认Activity
,因为您正在StartDraw
中扩展活动类。所以只需在Manifest中将默认活动名称更改为StartDraw
:
<activity
android:name=".StartDraw" <!--here use StartDraw instead Map -->
android:label="@string/app_name" >
<!-- other attributes here -->
</activity>
答案 2 :(得分:0)
只需更改androidmanifest.xml
而不是android:name="com.mapping.Map"
<activity android:name=".startdraw" android:label="@string/app_name" android:launchMode="singleTask">