【发布时间】:2011-05-06 01:14:23
【问题描述】:
我正在尝试设置屏幕上图像 (image2) 的大小和位置,使其与背景图像 (image1) 的位置和大小相同,无论屏幕大小如何。为此,我尝试在 Activity 类的 onCreate 方法中更改 image2 的布局参数:
public class Game_main extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ImageView image2= (ImageView) findViewById(R.id.seed1);
ImageView image1= (ImageView) findViewById(R.id.board_rev1);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlGame);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((image1.getWidth())/4, (image2.getHeight())/4);
params.leftMargin = (image1.getWidth())/2;
params.topMargin = (image1.getHeight())/4;
rl.addView(image2, params);
setContentView(R.layout.gamerev);
}
}
但是当我的应用程序进入此活动时,我收到一条错误消息,指出应用程序已意外停止。然后我必须强制关闭应用程序。为什么会发生这种情况?有什么我想念的吗?还是有更好的方法来做到这一点?
logcat中的错误是:
E/AndroidRuntime(296): java.lang.RuntimeException: Unable to start activity Co mponentInfo{com.soft.tobi/com.soft.tobi.Game_main}:java.lang.NullPointerExc 选项
但我不明白为什么任何内容都会为空,因为所有 id 都存在于我的 xml 文件中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlGame"
>
<ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/board_rev1" android:src="@drawable/board_rev1"></ImageView>
<ImageView android:id="@+id/seed1" android:src="@drawable/seed1" android:adjustViewBounds="true" android:layout_height="200dp" android:layout_marginTop="165dp" android:layout_width="200dp" android:layout_marginLeft="30dp"></ImageView>
</RelativeLayout>
谢谢
【问题讨论】:
-
你在哪里做这个? (如果在 onCreate 中它将不起作用,因为 getWidth 将返回零)。 logcat中的错误是什么?您没有提供足够的信息来帮助您。
-
我在 OnCreate 中执行此操作。我怎样才能得到logcat?我尝试像这样运行 $adb logcat:developer.android.com/guide/developing/tools/logcat.html,但它没有用。您还需要其他信息吗?
-
我已经更新了我的问题以包含所有必要的信息
标签: java android animation android-layout android-imageview