简体   繁体   English

如果原始资源文件夹不为空,则setContentView()返回null

[英]setContentView() returns null if raw resource folder is not empty

I have a main activity that starts a child activity. 我的主要活动是开始儿童活动。 In the childs onCreate() I call: 在孩子的onCreate()我称:

setContentView(R.layout.console);
TextView tv = (TextView)findViewById(R.id.console);

This works find as long as my raw resource folder is empty. 只要我的原始资源文件夹为空,就可以找到该文件。 When I put any file into it, findViewById() returns null . 当我将任何文件放入其中时, findViewById()返回null I cannot find out why there is an interference between files in the raw folder and the lack to find a layout resource. 我不知道为什么原始文件夹中的文件之间存在干扰,而缺乏查找布局资源的原因。

I tried to build with NetBeans and Eclipse and the R.java contains all necessary entries and no ID clash. 我尝试使用NetBeans和Eclipse进行构建,R.java包含所有必需的条目,并且没有ID冲突。 If I add a file in the raw folder, the file ID is added correctly in the R.java and nothing else changes. 如果我在原始文件夹中添加文件,则文件ID将正确添加到R.java中,并且没有其他更改。

In LogCat I get a null pointer exception as soon as I try to use the TextView tv. 在LogCat中,尝试使用TextView电视时,我立即得到一个空指针异常。 When I write the value of tv to the log immediately after its declaration/initialization, tv is the null reference. 当我在声明/初始化之后立即将tv的值写入日志时,tv是空引用。

@Override
protected void onCreate(Bundle savedInstanceState)
{
  Log.i("Calling onCreate()");
  super.onCreate(savedInstanceState);
  setContentView(R.layout.console);
  TextView tv = (TextView)findViewById(R.id.console);
  Log.i("tv = " + tv);
}

It seems to be a problem because the activity is lauched by the MAIN activity with 这似乎是一个问题,因为该活动由MAIN活动与

Intent intent = new Intent(getContext(), ConsoleActivity.class);
int requestCode = Activity.RESULT_OK;
startActivityForResult(intent, requestCode);

The layout file console.xml of the console activity is the following: 控制台活动的布局文件console.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

   <ScrollView
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

        <TextView
         android:id="@+id/console"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"/>
    </ScrollView>

   </LinearLayout>

</LinearLayout>

I found a workaround. 我找到了解决方法。 The code was part of a library. 该代码是库的一部分。 In the library project, I did not include the raw resource directory, because I do not use raw resources in the library. 在库项目中,我没有包括原始资源目录,因为我不在库中使用原始资源。 Now I created a raw directory with some (dummy) files in the library project, and... the null pointer error when adding a raw directory with files in the apps project disappeared miraculousely. 现在,我在库项目中创建了一个包含一些(虚拟)文件的原始目录,并且...在apps项目中添加了一个包含文件的原始目录时,空指针错误奇迹般地消失了。

Thanks anyway for your help that stimulated me. 无论如何,谢谢您的帮助,这刺激了我。

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

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