我现在在这个问题上花了4个多小时,我无法解决它!这是我的Java类:
public class Broken extends Activity implements View.OnClickListener {
@Override
protected void onResume() {
super.onResume();
final Globals g = (Globals)getApplication();
super.onResume();
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
protected void onPause() {
super.onPause();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Globals g = (Globals)getApplication();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
ScreenLayout();
}
public void ScreenLayout() {
final Globals g = (Globals)getApplication();
int screenHeight = (g.getScreenHeight());
int screenWidth = (g.getScreenWidth());
ImageView hmbg = (ImageView) this.findViewById(R.id.mapBackground);
final float SCALE = getResources().getDisplayMetrics().density;
int screenHeightDensity = (g.getScreenHeightDensity());
int screenWidthDensity = (g.getScreenWidthDensity());
RelativeLayout.LayoutParams btnBackgroundParams = new RelativeLayout.LayoutParams(hmbg.getLayoutParams());
btnBackgroundParams.topMargin = 100;
btnBackgroundParams.leftMargin = 100;
btnBackgroundParams.height = 50;
btnBackgroundParams.width = 50;
hmbg.setLayoutParams(btnBackgroundParams);
}
到目前为止非常简单的东西。这是我的XML文件:
RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:orientation="horizontal"
tools:context=".MainActivity"
android:id="@+id/Form_HordeMap">
<ImageView
android:id="@+id/mapBackground"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_green">
</ImageView>
</RelativeLayout>
每次启动此活动时应用程序都会崩溃,因为行:
RelativeLayout.LayoutParams btnBackgroundParams = new RelativeLayout.LayoutParams(hmbg.getLayoutParams());
抛出空指针异常。
我一直在阅读无数的论坛。我已经完成了100次这段代码,看不到我错过的任何内容。我只是无法通过这个问题。请帮助我,这样我就可以继续我的生活!
编辑:这里要求的是错误消息:
AndroidRuntime:FATAL EXCEPTION:主要 进程:rule02.touchpool,PID:31822 java.lang.RuntimeException:无法启动活动
ComponentInfo {rule02.touchpool / rule02.touchpool.HordeMap}:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.ViewGroup $ LayoutParams android.widget.ImageView.getLayoutParams()'
答案 0 :(得分:1)
您没有为Activity
设置布局。你必须使用setContentView例如
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutId);
ScreenLayout();
}