Just beginning to learn Android development, facing a simple example of the book to write again, and the results of running the simulator, always prompt Unfortunately, Activity has stopped., Very little code, want to know to be able to help solve what should be grateful .
First code MyView.java
package picksomething.activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class MyView extends View {
Paint paint;
public MyView (Context context) {
super (context);
paint.setColor (Color.WHITE);
paint.setTextSize (20);
paint.setAntiAlias (true);
}
protected void onDraw (Canvas canvas) {
super.onDraw (canvas);
canvas.drawColor (Color.GRAY);
canvas.drawRect (10, 10, 110, 110, paint);
canvas.drawText ("This interface is a custom View", 60, 170, paint);
}
}
Second code ActivityDemo.java
package picksomething.activity;
import android.os.Bundle;
import android.app.Activity;
public class ActivityDemo extends Activity {
MyView myView = new MyView (this);
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
this.setContentView (myView);
}
}
Java files on these two, because there is no design to layout files, layout files would not have posted it, the project name is Activity, always run to the simulator when prompted Unfortunately, Activity has stopped.
Reply:
The above information LogCat stickers out
Reply:
Reply:
I'm sorry, these days something I posted tomorrow.
Reply:
Thank you for pointing, plus ask where that file is added to the AndroidManifest.xml do
Reply:
AndroidManifest.xml is configured to increase activity
Reply:
lz is not no statement ActivityDemo
in the manifest file AndroidManifest.xml
Carefully check the operating procedures on the books mentioned under
Reply:
The amount of the above methods are tried, or why not. I posted about logcat inside the error, paste what AndroidManifest.xml file, you help look.
This is the wrong message.
03-25 13:15:26.109: E / Trace (937): error opening trace file: No such file or directory (2)
03-25 13:15:26.339: D / AndroidRuntime (937): Shutting down VM
03-25 13:15:26.399: W / dalvikvm (937): threadid = 1: thread exiting with uncaught exception (group = 0x40a71930)
03-25 13:15:26.459: E / AndroidRuntime (937): FATAL EXCEPTION: main
03-25 13:15:26.459: E / AndroidRuntime (937): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo {picksomething.activity / picksomething.activity.ActivityDemo}: java.lang.NullPointerException
03-25 13:15:26.459: E / AndroidRuntime (937): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2106)
03-25 13:15:26.459: E / AndroidRuntime (937): at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2230)
03-25 13:15:26.459: E / AndroidRuntime (937): at android.app.ActivityThread.access $ 600 (ActivityThread.java: 141)
03-25 13:15:26.459: E / AndroidRuntime (937): at android.app.ActivityThread $ H.handleMessage (ActivityThread.java: 1234)
03-25 13:15:26.459: E / AndroidRuntime (937): at android.os.Handler.dispatchMessage (Handler.java: 99)
03-25 13:15:26.459: E / AndroidRuntime (937): at android.os.Looper.loop (Looper.java: 137)
03-25 13:15:26.459: E / AndroidRuntime (937): at android.app.ActivityThread.main (ActivityThread.java: 5041)
03-25 13:15:26.459: E / AndroidRuntime (937): at java.lang.reflect.Method.invokeNative (Native Method)
03-25 13:15:26.459: E / AndroidRuntime (937): at java.lang.reflect.Method.invoke (Method.java: 511)
03-25 13:15:26.459: E / AndroidRuntime (937): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 793)
03-25 13:15:26.459: E / AndroidRuntime (937): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 560)
03-25 13:15:26.459: E / AndroidRuntime (937): at dalvik.system.NativeStart.main (Native Method)
03-25 13:15:26.459: E / AndroidRuntime (937): Caused by: java.lang.NullPointerException
03-25 13:15:26.459: E / AndroidRuntime (937): at android.content.ContextWrapper.getResources (ContextWrapper.java: 89)
03-25 13:15:26.459: E / AndroidRuntime (937): at android.view.ContextThemeWrapper.getResources (ContextThemeWrapper.java: 78)
03-25 13:15:26.459: E / AndroidRuntime (937): at android.view.View. (View.java: 3226)
03-25 13:15:26.459: E / AndroidRuntime (937): at picksomething.activity.MyView. (MyView.java: 11)
03-25 13:15:26.459: E / AndroidRuntime (937): at picksomething.activity.ActivityDemo. (ActivityDemo.java: 9)
03-25 13:15:26.459: E / AndroidRuntime (937): at java.lang.Class.newInstanceImpl (Native Method)
03-25 13:15:26.459: E / AndroidRuntime (937): at java.lang.Class.newInstance (Class.java: 1319)
03-25 13:15:26.459: E / AndroidRuntime (937): at android.app.Instrumentation.newActivity (Instrumentation.java: 1054)
03-25 13:15:26.459: E / AndroidRuntime (937): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2097)
03-25 13:15:26.459: E / AndroidRuntime (937): ... 11 more
03-25 13:15:30.619: I / Process (937): Sending signal PID:. 937 SIG: 9
This is AndroidManifest.xml file.
package = "picksomething.activity"
android: versionCode = "1"
android: versionName = "1.0">android: minSdkVersion = "8"
android: targetSdkVersion = "17" />android: allowBackup = "true"
android: icon = "@ drawable / ic_launcher"
android: label = "@ string / app_name"
android: theme = "@ style / AppTheme">android: name = "picksomething.activity.ActivityDemo"
android: label = "@ string / app_name">
Reply:
The list of documents
Need to change
Reply:
And the MyView (Context context) in the super (context); removal
Reply:
Amount, or not.
Reply:
Errors will be prompted to remove the phrase
Reply:
You do not have new paint it also
Reply:
I tried it, too.
Reply:
The super (context); into the last line of the method and the paint is initialized about ~
Reply:
The
MyView myView = new MyView (this);
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
this.setContentView (myView);
}
Changed:
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
MyView myView = new MyView (this);
this.setContentView (myView);
}
Ok, currently do not know why, I just started to learn android!
Reply:
Analysis of what that source because of: onCreate () function call activity before the object's constructor runs, this time the object has not been created, so myView point to null, is not referenced, so be initialized before referring to him, but also is again this.setContentView (myView); before!
No comments:
Post a Comment