我有一个奇怪的问题,我在layout-large-land中创建了一个xml,并且我已经放置了一些按钮 我已经为该xml文件编写了一个活动,然后检查它是否在大屏幕中是横向的 我写了点击按钮但不幸的是它说空指针异常我不明白我做错了什么,我正在检查我的三星Tab3
这是我的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/tabContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ScrollView
android:layout_width="90dp"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/topPick"
android:layout_width="match_parent"
android:layout_height="70dip"
android:layout_marginTop="100dip" />
<Button
android:layout_width="match_parent"
android:layout_height="70dip" />
<Button
android:layout_width="match_parent"
android:layout_height="70dip" />
<Button
android:layout_width="match_parent"
android:layout_height="70dip" />
<Button
android:layout_width="match_parent"
android:layout_height="70dip" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
这是我的活动
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
Button topPick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isTablet(MainActivity.this)) {
// tablet
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
topPick = (Button) findViewById(R.id.topPick);
topPick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "yyyyyyy", 2000)
.show();
}
});
} else {
// phone
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main);
}
}
public boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
}
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.screentest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.screentest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的logcat
06-21 15:41:44.900: E/AndroidRuntime(30469): FATAL EXCEPTION: main
06-21 15:41:44.900: E/AndroidRuntime(30469): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.screentest/com.example.screentest.MainActivity}: java.lang.NullPointerException
06-21 15:41:44.900: E/AndroidRuntime(30469): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
06-21 15:41:44.900: E/AndroidRuntime(30469): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
06-21 15:41:44.900: E/AndroidRuntime(30469): at android.app.ActivityThread.access$700(ActivityThread.java:154)
06-21 15:41:44.900: E/AndroidRuntime(30469): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
06-21 15:41:44.900: E/AndroidRuntime(30469): at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 15:41:44.900: E/AndroidRuntime(30469): at android.os.Looper.loop(Looper.java:137)
06-21 15:41:44.900: E/AndroidRuntime(30469): at android.app.ActivityThread.main(ActivityThread.java:5306)
06-21 15:41:44.900: E/AndroidRuntime(30469): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 15:41:44.900: E/AndroidRuntime(30469): at java.lang.reflect.Method.invoke(Method.java:511)
06-21 15:41:44.900: E/AndroidRuntime(30469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-21 15:41:44.900: E/AndroidRuntime(30469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-21 15:41:44.900: E/AndroidRuntime(30469): at dalvik.system.NativeStart.main(Native Method)
06-21 15:41:44.900: E/AndroidRuntime(30469): Caused by: java.lang.NullPointerException
06-21 15:41:44.900: E/AndroidRuntime(30469): at com.example.screentest.MainActivity.onCreate(MainActivity.java:27)
06-21 15:41:44.900: E/AndroidRuntime(30469): at android.app.Activity.performCreate(Activity.java:5255)
06-21 15:41:44.900: E/AndroidRuntime(30469): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-21 15:41:44.900: E/AndroidRuntime(30469): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
答案 0 :(得分:1)
您的代码工作正常。请参阅我在eclipse中编写的代码,并查看附带的屏幕截图。
XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/tabContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ScrollView
android:layout_width="90dp"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/topPick"
android:layout_width="match_parent"
android:layout_height="70dip"
android:layout_marginTop="100dip" />
<Button
android:layout_width="match_parent"
android:layout_height="70dip" />
<Button
android:layout_width="match_parent"
android:layout_height="70dip" />
<Button
android:layout_width="match_parent"
android:layout_height="70dip" />
<Button
android:layout_width="match_parent"
android:layout_height="70dip" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
JAVA文件
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
Button topPick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isTablet(MainActivity.this)) {
// tablet
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_main);
topPick = (Button) findViewById(R.id.topPick);
topPick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "yyyyyyy", 2000)
.show();
}
});
} else {
// phone
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
}
}
public boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
}