简体   繁体   English

Android:Findviewbyid()返回null

[英]Android: Findviewbyid() returns null

I am trying to make an app with three RelativeLayouts nested in one RelativeLayout. 我正在尝试使一个RelativeLayout中嵌套三个RelativeLayouts的应用程序。 I want to programmatically change the visibility of the RelativeLayouts. 我想以编程方式更改RelativeLayouts的可见性。 It works fine on my smartphone (Android 4.4.2), but not on tablet (Android 5.1.1) 它可以在我的智能手机(Android 4.4.2)上正常运行,但不能在平板电脑(Android 5.1.1)上正常运行

When findViewById() is called on the tablet, it returns null. 在平板电脑上调用findViewById() ,它返回null。

What am I doing wrong? 我究竟做错了什么?

MainActivity.java: 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: 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>

Log: 日志:

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.access$800(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.access$800(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) 

Your application is working on phones but not on tablets, this usually means a problem of resources. 您的应用程序只能在手机上使用,而不能在平板电脑上使用,这通常意味着资源问题。

The resources ( layout , values , ...) used by Android depends on the device used. Android使用的资源( layoutvalues ,...)取决于所使用的设备。 If there is a specific folder that match the device configuration (like large screen), the resource redefine in this folder will be used instead of the general folder. 如果存在与设备配置匹配的特定文件夹(如大屏幕),则将使用此文件夹中重新定义的资源,而不是常规文件夹。

There is multiple difference between phone/tablet (screen size, ratio, orientation, ...). 手机/平板电脑之间有多个区别(屏幕尺寸,比例,方向等)。

In your case, you might need to update or remove the layout main_activity.xml in a layout folder that match your tablet. 在您的情况下,您可能需要更新或删除与平板电脑匹配的layout folder中的布局main_activity.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM