简体   繁体   English

findViewById()返回null

[英]findViewById() returning null

I'm currently new to Android development and have run into a problem that previous research isn't helping me to solve. 我目前是Android开发的新手,遇到了一个以前的研究无法帮助我解决的问题。 I'm using findViewById() in an Android activity to grab a TextView object declared in the activity's corresponding xml fragment file like so: 我在Android活动中使用findViewById()来抓取在活动的相应xml片段文件中声明的TextView对象,如下所示:

public void scanLocalApk() {
    //get the list of apks in the default downloads directory
    ArrayList<File> foundAPKs = findApksInDownloadDirectory();
    //display the list of found apks
    String apkListString = "";
    if (foundAPKs != null)
        for (File f : foundAPKs)
            apkListString += (f.getName() + "\n"); 
    TextView displayApksTextView = (TextView) findViewById(R.id.display_apks);
    displayApksTextView.setText(apkListString);
    TextView apkToInstallTextView = (TextView) findViewById(R.id.label_apk_to_install);
    apkToInstallTextView.setVisibility(View.VISIBLE);
    EditText apkToInstallEditText = (EditText) findViewById(R.id.apk_to_install);
    apkToInstallEditText.setVisibility(View.VISIBLE);
}

A NullPointerException is being thrown at this line: displayApksTextView.setText(apkListString); 在此行引发NullPointerException:displayApksTextView.setText(apkListString); because the findViewById call in the line above it is returning null. 因为上面一行中的findViewById调用返回null。

The resource "display_apks" is defined in "fragment_scan_local_apk.xml" here: 资源“ display_apks”在“ fragment_scan_local_apk.xml”中定义:

<TextView android:id="@+id/display_apks"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

Previously mentioned solutions around the web have said to ensure that setContentView() is called above findViewById(), but in my code it is. 先前在网络上提到的解决方案已经说过,以确保setContentView()在findViewById()之上被调用,但是在我的代码中是这样。

Does anyone have any ideas as to what the problem could be? 有人对问题可能有什么想法吗?

Edit: As per request, I call setContentView() here 编辑:根据请求,我在这里调用setContentView()

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan_local_apk);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    //branch to a logic method
    scanLocalApk();
}

The Views in your Fragment's layout xml are being inflated into the Fragment's View hierarchy, which won't be added to the Activity's hierarchy until the onAttach() callback, so findViewById() in the context of the Activity will return null for those Views at the time that onCreate() is called. Fragment的布局xml中的Views会膨胀为Fragment的View层次结构,直到onAttach()回调之前,它不会被添加到Activity的层次结构中,因此Activity上下文中的findViewById()对于这些Views将返回null onCreate()调用的时间。

If you want to keep the Views in the Fragment, it would be better to call findViewById() on the View that is returned from the onCreateView() method in the Fragment, and move the Views' functionalities and references to the Fragment. 如果要将视图保留在Fragment中,最好在Fragment中的onCreateView()方法返回的View上调用findViewById() ,然后将View的功能和引用移到Fragment中。

If you don't want/need to use Fragments, you can move the Views in fragment_scan_local_apk.xml to activity_scan_local_apk.xml , and keep the rest of the code as it is. 如果您不想/不需要使用片段,可以将fragment_scan_local_apk.xml的视图移动到activity_scan_local_apk.xml ,并将其余代码保持原样。 If you decide to go with this option, you can remove the code for the PlaceholderFragment . 如果决定使用此选项,则可以删除PlaceholderFragment的代码。

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

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