Im trying to add a view which consist of a background and a moving cloud to a layout that has buttons and checkboxes. I tried getLayoutInflater to merge both of them togather but it forced close. here is MainActivity code:
public class MainActivity extends Activity{
MyCloud myView;
float x, y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = new MyCloud(this);
getWindow().setFormat(PixelFormat.RGBA_8888);
setContentView(myView);
View view = getLayoutInflater().inflate(R.layout.activity_main, null, false);
this.addContentView(view, null);
}
}
Here is the View activity:
public class MyCloud extends View{
Bitmap cloud;
int ChangingX;
int ChangingY;
int x, y;
BitmapFactory.Options options;
Bitmap preparedBitmap;
public MyCloud(Context context) {
// TODO Auto-generated constructor stub
super(context);
setFocusable(true);
options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = true;
options.inScaled = false;
options.inDither = false;
options.inPurgeable = true;
preparedBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.bg, options);
Log.d("bitmap", String.valueOf(preparedBitmap));
cloud = BitmapFactory.decodeResource(getResources(), R.drawable.cloud);
ChangingX = 50;
ChangingY = 50;
x = 0;
y = 0;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(preparedBitmap, 0, 0, null); // for background
canvas.drawBitmap(cloud, ChangingX , 50, null); // for cloud
if (ChangingX < canvas.getWidth())
ChangingX += 2;
else
ChangingX = 50;
invalidate();
}
}
The layout Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="170dp"
android:layout_marginRight="58dp"
android:text="Button" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginLeft="27dp"
android:text="CheckBox" />
</RelativeLayout>
The error I have:
06-15 12:34:20.801: E/AndroidRuntime(528): FATAL EXCEPTION: main
06-15 12:34:20.801: E/AndroidRuntime(528):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.cloud/com.cloud.MainActivity}: java.lang.NullPointerException
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.os.Looper.loop(Looper.java:123)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.app.ActivityThread.main(ActivityThread.java:4627)
06-15 12:34:20.801: E/AndroidRuntime(528):
at java.lang.reflect.Method.invokeNative(Native Method)
06-15 12:34:20.801: E/AndroidRuntime(528):
at java.lang.reflect.Method.invoke(Method.java:521)
06-15 12:34:20.801: E/AndroidRuntime(528):
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-15 12:34:20.801: E/AndroidRuntime(528):
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-15 12:34:20.801: E/AndroidRuntime(528):
at dalvik.system.NativeStart.main(Native Method)
06-15 12:34:20.801: E/AndroidRuntime(528): Caused by: java.lang.NullPointerException
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:3573)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3723)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:487)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:431)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.view.ViewGroup.addViewInner(ViewGroup.java:1975)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.view.ViewGroup.addView(ViewGroup.java:1865)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.view.ViewGroup.addView(ViewGroup.java:1845)
06-15 12:34:20.801: E/AndroidRuntime(528):
at com.android.internal.policy.impl.PhoneWindow.addContentView(PhoneWindow.java:229)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.app.Activity.addContentView(Activity.java:1681)
06-15 12:34:20.801: E/AndroidRuntime(528):
at com.cloud.MainActivity.onCreate(MainActivity.java:35)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-15 12:34:20.801: E/AndroidRuntime(528):
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-15 12:34:20.801: E/AndroidRuntime(528): ... 11 more
Now I have never used getLayoutInflater before so can you please be patient with me and show me in Code how I can fix this problem or if there is any alternative way to add the view to the layout. Thanks in advance