Android InFilter将视图添加到布局

Android InFilter将视图添加到布局,android,android-view,layout-inflater,Android,Android View,Layout Inflater,我试图添加一个由背景和移动云组成的视图到一个有按钮和复选框的布局中。我试着把他们两个合并在一起,但结果被迫关闭了。以下是主要活动代码: public class MainActivity extends Activity{ MyCloud myView; float x, y; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); m

我试图添加一个由背景和移动云组成的视图到一个有按钮和复选框的布局中。我试着把他们两个合并在一起,但结果被迫关闭了。以下是主要活动代码:

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);
}
}
以下是查看活动:

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();

}

}
公共类MyCloud扩展视图{
位图云;
int ChangingX;
国际变化;
int x,y;
选项;
位图预处理位图;
公共MyCloud(上下文){
//TODO自动生成的构造函数存根
超级(上下文);
设置聚焦(真);
options=新的BitmapFactory.options();
options.inPreferredConfig=Bitmap.Config.ARGB_8888;
options.inDither=true;
options.inScaled=false;
options.inDither=false;
options.inpurgable=true;
preparedBitmap=BitmapFactory.decodeResource(getResources(),
R.drawable.bg,选项);
Log.d(“位图”,String.valueOf(preparedBitmap));
cloud=BitmapFactory.decodeResource(getResources(),R.drawable.cloud);
ChangingX=50;
ChangingY=50;
x=0;
y=0;
}
@凌驾
受保护的void onDraw(画布){
//TODO自动生成的方法存根
super.onDraw(帆布);
drawBitmap(preparedBitmap,0,0,null);//用于背景
drawBitmap(cloud,ChangingX,50,null);//用于cloud
if(ChangingX
布局代码:

<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>

我的错误是:

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
06-15 12:34:20.801:E/AndroidRuntime(528):致命异常:main
06-15 12:34:20.801:E/AndroidRuntime(528):
java.lang.RuntimeException:无法启动活动
ComponentInfo{com.cloud/com.cloud.MainActivity}:java.lang.NullPointerException
06-15 12:34:20.801:E/AndroidRuntime(528):
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)上
06-15 12:34:20.801:E/AndroidRuntime(528):
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-15 12:34:20.801:E/AndroidRuntime(528):
在android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-15 12:34:20.801:E/AndroidRuntime(528):
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)上
06-15 12:34:20.801:E/AndroidRuntime(528):
位于android.os.Handler.dispatchMessage(Handler.java:99)
06-15 12:34:20.801:E/AndroidRuntime(528):
位于android.os.Looper.loop(Looper.java:123)
06-15 12:34:20.801:E/AndroidRuntime(528):
位于android.app.ActivityThread.main(ActivityThread.java:4627)
06-15 12:34:20.801:E/AndroidRuntime(528):
位于java.lang.reflect.Method.Invokenactive(本机方法)
06-15 12:34:20.801:E/AndroidRuntime(528):
位于java.lang.reflect.Method.invoke(Method.java:521)
06-15 12:34:20.801:E/AndroidRuntime(528):
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)上
06-15 12:34:20.801:E/AndroidRuntime(528):
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-15 12:34:20.801:E/AndroidRuntime(528):
在dalvik.system.NativeStart.main(本机方法)
06-15 12:34:20.801:E/AndroidRuntime(528):由以下原因引起:java.lang.NullPointerException
06-15 12:34:20.801:E/AndroidRuntime(528):
在android.view.ViewGroup$LayoutParams(ViewGroup.java:3573)
06-15 12:34:20.801:E/AndroidRuntime(528):
在android.view.ViewGroup$MarginLayoutParams(ViewGroup.java:3723)
06-15 12:34:20.801:E/AndroidRuntime(528):
位于android.widget.FrameLayout$LayoutParams(FrameLayout.java:487)
06-15 12:34:20.801:E/AndroidRuntime(528):
位于android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:431)
06-15 12:34:20.801:E/AndroidRuntime(528):
在android.view.ViewGroup.addViewInner(ViewGroup.java:1975)
06-15 12:34:20.801:E/AndroidRuntime(528):
在android.view.ViewGroup.addView(ViewGroup.java:1865)中
06-15 12:34:20.801:E/AndroidRuntime(528):
在android.view.ViewGroup.addView(ViewGroup.java:1845)中
06-15 12:34:20.801:E/AndroidRuntime(528):
在com.android.internal.policy.impl.PhoneWindow.addContentView(PhoneWindow.java:229)上
06-15 12:34:20.801:E/AndroidRuntime(528):
位于android.app.Activity.addContentView(Activity.java:1681)
06-15 12:34:20.801:E/AndroidRuntime(528):
位于com.cloud.MainActivity.onCreate(MainActivity.java:35)
06-15 12:34:20.801:E/AndroidRuntime(528):
位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-15 12:34:20.801:E/AndroidRuntime(528):
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)上
06-15 12:34:20.801:E/AndroidRuntime(528):。。。还有11个

现在,我以前从未使用过getLayoutInflater,因此请您耐心等待,并在代码中向我展示如何解决此问题,或者是否有其他方法将视图添加到布局中。提前感谢

在activity\u main.xml中进行线性布局或重新布局

myView = new MyCloud(this); 
View view = getLayoutInflater().inflate(R.layout.activity_main, null, false);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll);
ll.addView(myView); // add custom view to linealayout
setContentView(view); // set the inflated layout to activity
<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"
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" />
 <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/ll"
        android:layout_above="@+id/checkBox1"
        android:orientation="vertical"
      />
 </RelativeLayout>
充气活动_main.xml

myView = new MyCloud(this); 
View view = getLayoutInflater().inflate(R.layout.activity_main, null, false);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll);
ll.addView(myView); // add custom view to linealayout
setContentView(view); // set the inflated layout to activity
<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"
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" />
 <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/ll"
        android:layout_above="@+id/checkBox1"
        android:orientation="vertical"
      />
 </RelativeLayout>
activity_main.xml

myView = new MyCloud(this); 
View view = getLayoutInflater().inflate(R.layout.activity_main, null, false);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll);
ll.addView(myView); // add custom view to linealayout
setContentView(view); // set the inflated layout to activity
<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"
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" />
 <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/ll"
        android:layout_above="@+id/checkBox1"
        android:orientation="vertical"
      />
 </RelativeLayout>
在你的
相对论中

   android:background="@drawable/ic_launcher"  

您需要在布局中有按钮和复选框,以及xml中的按钮和复选框。你能检查一下上面的xml代码吗?我使用相对布局并使用了您的代码,但我使用的不是线性布局,而是相对布局,并在XMLcheck编辑中添加了一个ID。将您的线性布局放置在您喜欢的任何位置,然后将自定义视图的内容设置为线性布局。感谢您的努力,它起到了作用,但视图似乎只添加到线性布局中,因此我在线性布局中有视图背景,而在主活动中没有背景。将背景设置为您的视图。