繁体   English   中英

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

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

我的主要活动是开始儿童活动。 在孩子的onCreate()我称:

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

只要我的原始资源文件夹为空,就可以找到该文件。 当我将任何文件放入其中时, findViewById()返回null 我不知道为什么原始文件夹中的文件之间存在干扰,而缺乏查找布局资源的原因。

我尝试使用NetBeans和Eclipse进行构建,R.java包含所有必需的条目,并且没有ID冲突。 如果我在原始文件夹中添加文件,则文件ID将正确添加到R.java中,并且没有其他更改。

在LogCat中,尝试使用TextView电视时,我立即得到一个空指针异常。 当我在声明/初始化之后立即将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);
}

这似乎是一个问题,因为该活动由MAIN活动与

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

控制台活动的布局文件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>

我找到了解决方法。 该代码是库的一部分。 在库项目中,我没有包括原始资源目录,因为我不在库中使用原始资源。 现在,我在库项目中创建了一个包含一些(虚拟)文件的原始目录,并且...在apps项目中添加了一个包含文件的原始目录时,空指针错误奇迹般地消失了。

无论如何,谢谢您的帮助,这刺激了我。

暂无
暂无

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

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