[英]How to change the background by use Image_path and BitmapDrawable?
I want to change the background for Activity. 我想更改活动的背景。
I choose a picture in Fragment
. 我在Fragment
选择一张图片。
Use intent and transmit the path to Activity. 使用意图并将路径传输到活动。
The Activity receive the path but when I set the background for the Activity , it crash. Activity接收路径,但是当我为Activity设置背景时,它崩溃了。
The following code is in Activity: 以下代码位于“活动”中:
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) ;
And the error log is like the following: 错误日志如下所示:
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
The tw.com.a_i_t.IPCamViewer.Splash.onCreate(Splash.java:47)
is relativeLayout.setBackgroundDrawable(background);
tw.com.a_i_t.IPCamViewer.Splash.onCreate(Splash.java:47)
是relativeLayout.setBackgroundDrawable(background);
Does someone can teach me how to solve this problem ? 有人可以教我如何解决这个问题吗?
The reason is 原因是
setContentView(R.layout.splash) ;
Then initialize views. 然后初始化视图。 But you have it at the last 但是你终于有了
setContentView(R.layout.splash) ; // first set the layout to the activity
relativeLayout = (RelativeLayout) findViewById(R.id.splash); // then initialize your views
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.