@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get orientation
LinearLayout layout = isLandscape()
? (LinearLayout) this.findViewById(R.id.main_layout_l)
: (LinearLayout) this.findViewById(R.id.main_layout_p);
Log.i("info", "layout is " + layout.getId());
start();
run();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.e("On Config Change",
(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
? "LANDSCAPE" : "PORTRAIT");
// added -- this fixes the background not showing
setContentView(R.layout.activity_main);
}
我有两个方向布局(两个文件,layout / activity_main.xml,layout-land / activity.xml)。根据方向,显示的内容略有不同,即:肖像不会有一些额外的面板,一些东西必须移动。我需要以编程方式访问视图。
访问'layout'时,我得到一个NullPointerException
每个布局方向都有不同的id,以xml文件中的“_p”或“_l”结尾,以区分纵向或横向,在java中访问它们。
两个xml文件当前都是LinearLayout-> TextView无论哪种方式我都无法绘制它们
我正在使用线程和时钟来处理绘图调用。喜欢在游戏中。这是进行各种自定义动画所必需的。
我甚至认为背景不是画画。
如果我从LinearLayout更改并直接通过其id调用TextView,它运行没有错误,但没有任何绘制。
尝试删除android:configChanges
属性。
最好不要使用android:configChanges
属性。
请参阅Android开发者网站的详细信息
Caution: Handling the configuration change yourself can make it much more difficult to
use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
https://developer.android.com/guide/topics/resources/runtime-changes