Android开发设置背景

时间:2013-11-27 18:04:55

标签: android android-debug

我想通过改变主要活动来改变背景照片这里的java代码是我写的。当我在我的galaxy s3上运行应用程序时,当我点击按钮时,我的手机卡住并退出应用程序

这是xml代码:

<RelativeLayout android:id="@+id/helpLayout"
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/bgr"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:textStyle="bold"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/How" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_centerVertical="true"
        android:onClick="onRadioButtonClicked"
        android:text="Click Me :)" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="26dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:textColor="#FFFF00"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Happy" />

        <RadioButton
            android:id="@+id/radio1"
            android:textColor="#000000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sad" />

        <RadioButton
            android:id="@+id/radio2"
            android:textColor="#FF0000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="In Love" />

        <RadioButton
            android:id="@+id/radio3"
            android:textColor="#808080"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Missing" />
    </RadioGroup>

</RelativeLayout>

这是java代码:

package com.example.anny;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void onRadioButtonClicked(View view) {
         LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main);
         ll.setBackgroundResource(R.drawable.p1);

    }
}

2 个答案:

答案 0 :(得分:1)

在您的代码onRadioButtonclicked中:

LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main);//parameter is layout instead of Id.
 ll.setBackgroundResource(R.drawable.p1);

首先,您的XML文件中没有LinearLayout。容器是RelativeLayout。其次,您提供了布局参考,而不是 ID 。在Android中,我们提供 ID R.id.IdName。您的XML代码中RelativeLayout ID helpLayout。所以,代码应该是:

RelativeLayout ll = (RelativeLayout) findViewById(R.id.helpLayout);

答案 1 :(得分:0)

使用此代码更改onRadioButtonClicked的代码,那么您的问题肯定会解决:

 RelativeLayout ll = (RelativeLayout) findViewById(R.id.helpLayout);
     ll.setBackgroundResource(R.drawable.p1);