我正在自定义我的应用程序以在平板电脑上显示为 2 列。我将左右列的宽度设置为setLayoutParams
. 它在 Android 3.0 和 Android 3.1 模拟器上运行良好。在 Android 3.2 模拟器上,它提供了一个nullPointerException
.
这是我的代码的一部分:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Debug.LogCat(activityName + ":OnCreate");
setContentView(R.layout.home);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
// set layout for tablets
final boolean isTablet = getResources().getBoolean(R.bool.isTablet);
if (isTablet) {
// set screenorientation to landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
// get half screensize
int width = metrics.heightPixels / 2;
// set left column to width
LinearLayout left_column = (LinearLayout) findViewById(R.id.left_column);
LinearLayout.LayoutParams lp_left_column = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT);
// this is where the NPE occurs
left_column.setLayoutParams(lp_left_column);
// set right column to width
LinearLayout right_column = (LinearLayout) findViewById(R.id.right_column);
LinearLayout.LayoutParams lp_right_column = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT);
right_column.setLayoutParams(lp_right_column);
}
这是我的布局(home.xml),我在其中指定了 id 的 left_column 和 right_column:
<LinearLayout 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:orientation="horizontal"
android:gravity="center" >
<LinearLayout
android:id="@+id/left_column"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_largeImage"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/largeImage"
android:contentDescription="@string/largeImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:onClick="playSound" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/right_column"
android:layout_width="0dip"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
android:visibility="visible"
android:layout_weight="1" >
<ImageView
android:id="@+id/infoImage"
android:contentDescription="@string/largeImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</LinearLayout>
有谁知道它为什么nullPointerException
在Android 3.2上给出一个?
编辑:2013 年 6 月 3 日:正如我在对 dymmeh 的回答中所说,布尔 isTablet 设置在 layouts.xml 文件夹值、values-large 和 values-xlarge 中。对 logcat 的调试显示它正在 if 语句内部,因此该部分正在工作。但在此之后,我在 2 个文件夹中也有一个 home.xml:layout 和 layout-land。事实证明,对于 Android 3.2,它从布局文件夹中获取 home.xml,而不是从布局域中获取。我试图在 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) 之后将 setContentView(R.layout.home) 放在 if 语句中,但它仍然给出 NPE。所以我继续搜索为什么 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) 不适用于 Android 3.2。
欢迎任何想法。