I have a self-define view class, I would like to add this view into
the layout , but there is a problem
02-21 17:23:07.843: ERROR/AndroidRuntime(1253): Uncaught handler:
thread main exiting due to uncaught exception
02-21 17:23:07.853: ERROR/AndroidRuntime(1253):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.helloandroid/
com.example.helloandroid.HelloAndroid}: java.lang.NullPointerException
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2401)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2417)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
android.app.ActivityThread.access$2100(ActivityThread.java:116)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
android.os.Handler.dispatchMessage(Handler.java:99)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
android.os.Looper.loop(Looper.java:123)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
android.app.ActivityThread.main(ActivityThread.java:4203)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
java.lang.reflect.Method.invokeNative(Native Method)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
java.lang.reflect.Method.invoke(Method.java:521)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
dalvik.system.NativeStart.main(Native Method)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): Caused by:
java.lang.NullPointerException
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:16)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1123)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2364)
02-21 17:23:07.853: ERROR/AndroidRuntime(1253): ... 11 more
Here is my code, I want the SelfView to be under the TextView.
public class HelloAndroid extends Activity {
private SelfView mSelfView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mlayout =
(LinearLayout)findViewById(R.id.mylayout);
mSelfView = new SelfView(this);
mlayout.addView(mSelfView);
setContentView(R.layout.main);
}
}
public class SelfView extends View {
public SelfView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mylayout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en