Android 如何使用Image_path和BitmapDrawable更改背景?

Android 如何使用Image_path和BitmapDrawable更改背景?,android,background,bitmap,Android,Background,Bitmap,我想更改活动的背景 我在片段中选择了一张图片 使用意图并将路径传输到活动 活动接收路径,但当我为活动设置背景时,它崩溃 以下代码处于活动中: public class Splash extends Activity { public static final String BACKGROUND_PATH = "path"; private String Background_path; private static final String TAG = "MJPEG

我想更改活动的背景

我在
片段中选择了一张图片

使用意图并将路径传输到活动

活动接收路径,但当我为活动设置背景时,它崩溃

以下代码处于活动中:

 public class Splash extends Activity {

    public static final String BACKGROUND_PATH = "path";
    private String Background_path;
    private static final String TAG = "MJPEG Player" ;
    private RelativeLayout relativeLayout;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState) ;


        // Hides the titlebar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE) ;

        final Intent intent = getIntent();
        Background_path = intent.getStringExtra(BACKGROUND_PATH);
        Log.i(TAG, "Background_path = " + Background_path);

        relativeLayout = (RelativeLayout) findViewById(R.id.splash);

        if(Background_path != null) {
            BitmapDrawable background = new BitmapDrawable(Background_path);
            relativeLayout.setBackgroundDrawable(background);
            Log.i(TAG, "background = " + background);
        }

    setContentView(R.layout.splash) ;
错误日志如下所示:

D/AndroidRuntime(20747): Shutting down VM
W/dalvikvm(20747): threadid=1: thread exiting with uncaught exception (group=0x416cc450)
E/AndroidRuntime(20747): FATAL EXCEPTION: main
E/AndroidRuntime(20747): java.lang.RuntimeException: Unable to start activity ComponentInfo{tw.com.a_i_t.IPCamViewer/tw.com.a_i_t.IPCamViewer.Splash}: java.lang.NullPointerException
E/AndroidRuntime(20747):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
E/AndroidRuntime(20747):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
E/AndroidRuntime(20747):    at android.app.ActivityThread.access$600(ActivityThread.java:137)
E/AndroidRuntime(20747):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
E/AndroidRuntime(20747):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(20747):    at android.os.Looper.loop(Looper.java:213)
E/AndroidRuntime(20747):    at android.app.ActivityThread.main(ActivityThread.java:4786)
E/AndroidRuntime(20747):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(20747):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(20747):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime(20747):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/AndroidRuntime(20747):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(20747): Caused by: java.lang.NullPointerException
E/AndroidRuntime(20747):    at tw.com.a_i_t.IPCamViewer.Splash.onCreate(Splash.java:47)
E/AndroidRuntime(20747):    at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime(20747):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime(20747):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
E/AndroidRuntime(20747):    ... 11 more
W/System.err(20747): java.io.FileNotFoundException: http://192.168.1.1/cgi-bin/liveMJPEG
W/ActivityManager(  568):   Force finishing activity tw.com.a_i_t.IPCamViewer/.Splash
W/ActivityManager(  568):   Force finishing activity tw.com.a_i_t.IPCamViewer/.MainActivity
tw.com.a_i_t.IPCamViewer.Splash.onCreate(Splash.java:47)
relativeLayout.setBackgroundDrawable(背景)

有人能教我如何解决这个问题吗?

原因是

 setContentView(R.layout.splash) ;
然后初始化视图。但你终于有了它

setContentView(R.layout.splash) ; // first set the layout to the activity
relativeLayout = (RelativeLayout) findViewById(R.id.splash); // then initialize your views

谢谢但当我关闭应用程序时,背景会回到原来的图片。如何记住新的背景?@MartinWun您可以使用共享首选项保存路径,再次获取路径并将其设置为背景。