如何使用Image_path和BitmapDrawable更改背景



我想更改活动的背景。

我在Fragment中选择一张图片。

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

活动

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

以下代码位于"活动"中:

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

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

原因是

 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

最新更新