大布局中按钮中的空指针

乔治·托马斯

我有一个奇怪的问题,我在大布局内创建了一个xml,并放置了一些按钮,并为该xml文件编写了一个活动,然后在大屏幕上检查它是否为横向,然后我写了click到按钮,但不幸的是,它说空指针异常我不明白我在做什么错,我正在用我的三星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>

这是我的日志

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)
CodeWarrior

您的代码运行正常。查看我在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);
    }
}

截屏

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章