美好的一天,我想允许用户更改我的应用中的背景。尝试制作它,但面对这个问题:
12-09 06:22:15.874 20413-20413/com.vadimsleonovs.horoscope E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vadimsleonovs.horoscope, PID: 20413
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/20 flg=0x1 }} to activity {com.vadimsleonovs.horoscope/com.vadimsleonovs.horoscope.activities.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setBackground(android.graphics.drawable.Drawable)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3539)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
at android.app.ActivityThread.access$1300(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setBackground(android.graphics.drawable.Drawable)' on a null object reference
at com.vadimsleonovs.horoscope.activities.SettingsActivity.onActivityResult(SettingsActivity.java:76)
at android.app.Activity.dispatchActivityResult(Activity.java:6135)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
at android.app.ActivityThread.access$1300(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
代码是:
public class SettingsActivity extends AppCompatActivity {
Button mBcgBtn;
private static int RESULT_LOAD_IMAGE = 1;
String picturePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mBcgBtn = (Button) findViewById(R.id.bcg_clr_btn);
mBcgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
try {
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
}catch(NullPointerException e){
e.printStackTrace();
}
LinearLayout mHomeLayout = (LinearLayout) findViewById(R.id.activity_home);
LinearLayout mDetailsPageLayout = (LinearLayout) findViewById(R.id.activity_details_page);
Drawable d = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(picturePath));
mHomeLayout.setBackground(d);
}
}
}
此外,我添加了写入外部存储权限,此代码在"空白项目"中完美运行,但在我的项目中根本不工作。
答案 0 :(得分:1)
它表示 mHomeLayout 为空。即, activity_settings 布局中没有ID activity_home 的LinearLayout。
因此,您需要做的是从“设置活动”中将选定的可绘制路径保存在存储首选项中。当您打开具有id activity_home 的Linearlayout的其他活动时,请加载它。然后将其设置为它的背景。
我希望这是你的问题。