1

我想将 bx.xml 中的图像视图发送到 another.xml(activity_main) 中的布局,并在 .xml (activity_main) 中带有按钮内容。这是 bx.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/ic_launcher30005"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="200dp"
        android:layout_height="200dp"

        android:src="@drawable/ic_launcher22" />

    <Button android:id="@+id/bSetWall"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="400dp"
        android:gravity="center"
        android:text="Imposta come sfondo"

        />
</LinearLayout>

在另一个。我有按钮的资源,还有它需要去imageview的布局,是这个:按钮,还有它需要去imageview的布局,是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="702dp"
            android:layout_gravity="left"
            android:orientation="vertical" >

    <Button
        android:id="@+id/bopentx"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="tattoo" />

    <Button
        android:id="@+id/bopensax"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="sand" />

     <Button
         android:id="@+id/bopenwx"
         android:layout_width="100dp"
         android:layout_height="100dp"
         android:layout_gravity="left"
         android:text="wet" />

    <Button
        android:id="@+id/bopennx"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="naked" />

    <Button
        android:id="@+id/Bopenlx"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="loincloth" />
    <Button
        android:id="@+id/Bopenbx"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="brazilian" />

     </LinearLayout>
    </ScrollView>

    <ScrollView
        android:id="@+id/Scroll1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right" >

        <LinearLayout
            android:id="@+id/Linear1"
            android:layout_width="wrap_content"
            android:layout_height="702dp"
            android:layout_gravity="right"
            android:orientation="vertical" >

 //the image needs to go here when i click the button


        </LinearLayout>
    </ScrollView>

</LinearLayout>

在菜单类中我使用了这个:

public class menu extends Activity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     Button button1=(Button) findViewById(R.id.bopennx);

    button1.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            switch (v.getId()){
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(R.layout.bx, null);  

                LinearLayout imageScrollLayout = (LinearLayout)view.findViewById(R.id.Linear1);
                ImageView  newImageView = (ImageView) view.findViewById(R.id.imageView1);
                newImageView.setImageResource(R.id.imageView1);
                imageScrollLayout.addView(newImageView);
                break

}
        }});
        }

logcat中的问题是:

FATAL EXCEPTION: main
java.lang.NullPointerException
at org.culiperfettihd.menu$1.onClick(menu.java:67)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17267)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

您不能将图像资源设置为ImageView它必须是drawable. 如果您希望新创建ImageView的图像与第一个图像具有相同的图像,请更改

newImageView.setImageResource(R.id.imageView1);

newImageView.setImageResource(R.drawable.ic_launcher22);

您实际上不能将它放在layout运行时。您可以按照自己的方式添加它,但您需要使用有效的resource id而不是另一个view

于 2013-05-22T19:39:14.297 回答