如何通过文件路径更改活动的背景?

时间:2014-01-01 05:33:44

标签: android android-intent android-fragments background

APP的情况如下:

Splash Activity将运行背景3秒钟(成为开场画面)。

Splash Activity使用startActivity来调用MainActivity,并完成自身。然后,MainActivity添加片段。

在这个片段中,它将选择一张图片并通过Uri uri = data.getData();来获取uri。

Fragment使用Intent和startActivity将此uri传输到第一个Activity(Splash Activity)。

Splash Activity接收uri并通过SharedPreferences保存。

Splash Activity使用此uri设置背景。

但它有错误

Splash Activity中的代码如下所示:

public class Splash extends Activity {

    public static final String BACKGROUND_PATH = "path";
    private Context mContext;

    //private static String PATH;
    private String Background_path;
    private String temp;
    private static final String TAG = "MJPEG Player" ;
    private RelativeLayout relativeLayout;

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


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

        setContentView(R.layout.splash) ;

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

        //If the image path receive from fragment is not empty , then save it.
        if(Background_path != null) {
            Log.i(TAG, "Background_path = " + Background_path);
            SharedPreferences settings = getSharedPreferences("PATH", 0);
            settings.edit()
                .putString("name", Background_path)
                .commit();
        }

         //if the value in SharedPreferences is not empty , set it to the background

        relativeLayout = (RelativeLayout)findViewById(R.id.splash);
        SharedPreferences settings = getSharedPreferences("PATH", 0);
        String temp = settings.getString("name", "");
        if(temp != null) {
            Log.i(TAG, "temp = " + temp);
            Uri uri = Uri.parse(temp);  
            ContentResolver cr = mContext.getContentResolver();
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
                BitmapDrawable background = new BitmapDrawable(bitmap);
                relativeLayout.setBackground(background);
            } catch (FileNotFoundException e) {
                // TODO: handle exception
            }
        }

错误日志是:

E/AndroidRuntime(32541): FATAL EXCEPTION: main
E/AndroidRuntime(32541): 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(32541):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
E/AndroidRuntime(32541):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
E/AndroidRuntime(32541):    at android.app.ActivityThread.access$600(ActivityThread.java:137)
E/AndroidRuntime(32541):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
E/AndroidRuntime(32541):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(32541):    at android.os.Looper.loop(Looper.java:213)
E/AndroidRuntime(32541):    at android.app.ActivityThread.main(ActivityThread.java:4786)
E/AndroidRuntime(32541):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(32541):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(32541):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime(32541):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/AndroidRuntime(32541):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(32541): Caused by: java.lang.NullPointerException
E/AndroidRuntime(32541):    at tw.com.a_i_t.IPCamViewer.Splash.onCreate(Splash.java:72)
E/AndroidRuntime(32541):    at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime(32541):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime(32541):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
E/AndroidRuntime(32541):    ... 11 more
W/ActivityManager(  568):   Force finishing activity tw.com.a_i_t.IPCamViewer/.Splash

Splash.java:72处的代码为ContentResolver cr = mContext.getContentResolver();

如何解决这个问题?

提前完成。

1 个答案:

答案 0 :(得分:1)

您尚未初始化mContext变量。以及为什么mContextActivityActivity Context mContext.getContentResolver() ...

因此您可以将this.getContentResolver()替换为{{1}}