设置背景图像不起作用

时间:2013-05-21 06:33:23

标签: android

我正在尝试为相对布局设置背景图像,但发生了一些错误 这是我的代码,提前谢谢

relative_topheader = new RelativeLayout(this);
relative_topheader.setId(1);
RelativeLayout.LayoutParams lp_topheader = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Resources resources_topheader = getResources();
Drawable drawable_topheader = resources_topheader.getDrawable(R.drawable.headerbar_m);
relative_topheader.setBackground(drawable_topheader);
setContentView(1);

我的日志猫

    05-21 11:40:05.655: I/dalvikvm(510): Could not find method android.widget.RelativeLayout.setBackground, referenced from method com.example.metromatineeapplicationlayer.MainActivity.onCreate
05-21 11:40:05.698: W/dalvikvm(510): VFY: unable to resolve virtual method 3324: Landroid/widget/RelativeLayout;.setBackground (Landroid/graphics/drawable/Drawable;)V
05-21 11:40:05.698: D/dalvikvm(510): VFY: replacing opcode 0x6e at 0x0052
05-21 11:40:05.788: D/AndroidRuntime(510): Shutting down VM
05-21 11:40:05.788: W/dalvikvm(510): threadid=1: thread exiting with uncaught exception (group=0x40014760)
05-21 11:40:05.795: E/AndroidRuntime(510): FATAL EXCEPTION: main
05-21 11:40:05.795: E/AndroidRuntime(510): java.lang.NoSuchMethodError: android.widget.RelativeLayout.setBackground
05-21 11:40:05.795: E/AndroidRuntime(510):  at com.example.metromatineeapplicationlayer.MainActivity.onCreate(MainActivity.java:60)
05-21 11:40:05.795: E/AndroidRuntime(510):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
05-21 11:40:05.795: E/AndroidRuntime(510):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
05-21 11:40:05.795: E/AndroidRuntime(510):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
05-21 11:40:05.795: E/AndroidRuntime(510):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
05-21 11:40:05.795: E/AndroidRuntime(510):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
05-21 11:40:05.795: E/AndroidRuntime(510):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:40:05.795: E/AndroidRuntime(510):  at android.os.Looper.loop(Looper.java:132)
05-21 11:40:05.795: E/AndroidRuntime(510):  at android.app.ActivityThread.main(ActivityThread.java:4025)
05-21 11:40:05.795: E/AndroidRuntime(510):  at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:40:05.795: E/AndroidRuntime(510):  at java.lang.reflect.Method.invoke(Method.java:491)
05-21 11:40:05.795: E/AndroidRuntime(510):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-21 11:40:05.795: E/AndroidRuntime(510):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-21 11:42:44.945: E/AndroidRuntime(546): FATAL EXCEPTION: main
05-21 11:42:44.945: E/AndroidRuntime(546): java.lang.NoSuchMethodError: android.widget.RelativeLayout.setBackground
05-21 11:42:44.945: E/AndroidRuntime(546):  at com.example.metromatineeapplicationlayer.MainActivity.onCreate(MainActivity.java:60)
05-21 11:42:44.945: E/AndroidRuntime(546):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
05-21 11:42:44.945: E/AndroidRuntime(546):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
05-21 11:42:44.945: E/AndroidRuntime(546):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
05-21 11:42:44.945: E/AndroidRuntime(546):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
05-21 11:42:44.945: E/AndroidRuntime(546):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
05-21 11:42:44.945: E/AndroidRuntime(546):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:42:44.945: E/AndroidRuntime(546):  at android.os.Looper.loop(Looper.java:132)
05-21 11:42:44.945: E/AndroidRuntime(546):  at android.app.ActivityThread.main(ActivityThread.java:4025)
05-21 11:42:44.945: E/AndroidRuntime(546):  at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:42:44.945: E/AndroidRuntime(546):  at java.lang.reflect.Method.invoke(Method.java:491)
05-21 11:42:44.945: E/AndroidRuntime(546):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-21 11:42:44.945: E/AndroidRuntime(546):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-21 11:42:44.945: E/AndroidRuntime(546):  at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

您的错误表示找不到setBackground

relativeLayout方法

你必须使用

relative_topheader.setBackgroundDrawable (drawable_topheader);

这将有所帮助

答案 1 :(得分:0)

试试这段代码

public void getpath() {
		File f = new File(new ImageLoader().fullPath + "/desiredFilename.png");
		if (f.exists()) {

			Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
			Resources res = this.getResources();
			BitmapDrawable bitmapDrawable = new BitmapDrawable(res, bitmap);
			int sdk = android.os.Build.VERSION.SDK_INT;
			System.out.println(sdk);
			if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
				System.out.println("less");
				bg.setBackgroundDrawable(bitmapDrawable);

			} else {
				System.out.println("great");
				bg.setBackground(bitmapDrawable);

			}
		} else {
			bg.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.phils));
		}

	}

答案 2 :(得分:0)

试试这个。

C:\\Users\\Agnisys\\Downloads\\Hopstarter-Sleek-Xp-Basic-Files.ico