Android: Findviewbyid() returns 空

Android: Findviewbyid() returns null

我正在尝试制作一个应用程序,其中三个 RelativeLayouts 嵌套在一个 RelativeLayout 中。我想以编程方式更改 RelativeLayouts 的可见性。它在我的智能手机 (Android 4.4.2) 上运行良好,但在平板电脑 (Android 5.1.1)

上运行不正常

当在平板电脑上调用 findViewById() 时,它 returns 为空。

我做错了什么?

MainActivity.java:

RelativeLayout mainactivity, multiplewifi, nowifi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState != null) //if switched from landscape to portrait mode or vice versa
        ((WebView) findViewById(R.id.webvieew)).restoreState(savedInstanceState); //restore webview
    else {
        Initialize();
        wifiManager.startScan(); //start a scan for available wifi networks
    }
}

public void Initialize() {
    mainactivity = (RelativeLayout) findViewById(R.id.MainActivity);
    multiplewifi = (RelativeLayout) findViewById(R.id.MultipleWifi);
    nowifi = (RelativeLayout) findViewById(R.id.NoWifi);
}

activity_main.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">

<RelativeLayout
    android:id="@+id/MultipleWifi">

    <Button/>

    <TextView/>

    <Spinner/>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/MainActivity">

    <WebView
    android:id="@+id/webvieew"/>

    <ImageButton/>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/NoWifi">

    <TextView/>
</RelativeLayout>

日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.wdt, PID: 5243
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wdt/com.example.wdt.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397)
                  at android.app.ActivityThread.access0(ActivityThread.java:151)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5268)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference
                  at com.example.wdt.MainActivity.Initialize(MainActivity.java:95)
                  at com.example.wdt.MainActivity.onCreate(MainActivity.java:75)
                  at android.app.Activity.performCreate(Activity.java:6033)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397) 
                  at android.app.ActivityThread.access0(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5268) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 

您的应用程序在手机上运行但在平板电脑上运行不正常,这通常意味着资源问题。

Android 使用的资源(layoutvalues、...)取决于所使用的设备。如果有匹配设备配置的特定文件夹(如大屏),将使用此文件夹中的资源重新定义,而不是一般文件夹。

phone/tablet(屏幕尺寸、比例、方向……)之间存在多重差异。

在您的情况下,您可能需要更新或删除 layout folder 中与您的平板电脑相匹配的布局 main_activity.xml