【问题标题】:Fail on trying to findViewById for nested TextView (within ListView)尝试查找嵌套 TextView 的 findViewById 失败(在 ListView 内)
【发布时间】:2011-03-23 13:04:26
【问题描述】:

试图拥有由两个 TextView 组成的行的 ListView(可滚动列表)。我有一个来自数据库的项目列表,我想将其填充到 LinearLayout->ListView->TextView 但无法获取 id...

Layout 有点像这个教学链接,但是已经放弃了 RelativeLayout 并使用 LinearLayout 来让它工作。甚至不担心它的外观;只是还不能把它连在一起。

http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

有两个 XML 文件(下面非常简短的详细信息) main.xml 和 stuff.xml

main.xml 有

...文本视图

...列表视图

stuff.xml 有

... android:id="@+id/firstLine"

... android:id="@+id/secondLine"

是的,我确实 setContentView(R.layout.main);在 onCreate 之后。

在 findViewByID(firstLine) 和 findViewID(secondLine) 上获取 null。

我有一个 ArrayAdapter,我在其中为 stuffView 充气。我对其他示例的思考和理解是,在我故意膨胀之前,它不会膨胀(这个嵌套的 stuffView)。一切正常,但是当我执行 findViewById 时,它返回 null,因此我无法 setText()。

由于我完全无知/新手,史诗般的失败。注意:我已经仔细阅读了 Reto 的书,尤其是第 163 页上的一个类似示例但失败失败失败......

有好心人能指引我正确的方向吗?

我必须膨胀这个嵌套视图吗? (Reto 的例子就是这样)。如果是这样,我错过了什么?我希望有人能给我指出一个更好的例子。我的代码在这一点上可能过于复杂而无法发布并且有点专有。

谢谢

main.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"
 >
 <TextView 
android:id="@+id/identText"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="some text here"

  />
 <ListView 
    android:id="@+id/myListView"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
  />

 </LinearLayout>

thing_item.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"
  >
<TextView 
android:id="@+id/identText"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="some text here"

  />
 <ListView 
    android:id="@+id/myListView"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
  />

</LinearLayout>

一个叫做 Thingy 的 pojo(这里不复制 Thingy.java - 非常简单)

主类:ShowLayoutNicely.java

package com.blap.test;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

public class ShowLayoutNicely extends Activity {
 ListView myListView;
 TextView myTextView;

ArrayList<Thingy> thingys;

ThingyAdapter aa;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myListView = (ListView) findViewById(R.id.myListView);
    myTextView = (TextView) findViewById(R.id.identText);

    thingys = new ArrayList<Thingy>();

    aa = new ThingyAdapter(this, android.R.layout.simple_list_item_1, thingys);
    myListView.setAdapter(aa);

// real deal has a call to go find items from database, populate array and notify of change.      
    Thingy thing1 = new Thingy("first", "first row");
    thingys.add(thing1);
// sadly - this isn't triggering getView() which I've overriden...
    aa.notifyDataSetChanged();

    Thingy thing2 = new Thingy("second", "second row");
    thingys.add(thing2);
    aa.notifyDataSetChanged();

}
}

适配器重写类 ThingyAdapter.java

package com.blap.test;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


public class ThingyAdapter extends ArrayAdapter<Thingy> {

int resource;

public ThingyAdapter (Context _context, int _resource, List<Thingy> _items){
super( _context, _resource, _items);
resource = _resource;

}

@Override
public View getView(int position, View convertView, ViewGroup parent)  {
LinearLayout thingView;

Thingy thingItem = getItem(position);

String identString = thingItem.getIdent();
String nameString =thingItem.getName();

if (convertView == null){
    thingView= new LinearLayout(getContext());
    LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View nuView = vi.inflate(resource, thingView, true);

}
else{
    thingView = (LinearLayout) convertView;

}
//here's the problem - these calls return nulls.
TextView row1 = (TextView)thingView.findViewById(R.id.firstLine);
TextView row2 = (TextView)thingView.findViewById(R.id.secondLine);

//and naturally these puke ingloriously....
row1.setText(thingItem.getIdent());
row2.setText(thingItem.getName());
return thingView;
}
}

所以这段代码本质上就是我正在寻求帮助的内容;将名称中性化为 Thingy.... 此示例未触发 getView()。这是我必须解决的次要问题。更重要的是,您对 findViewById 失败的帮助以及如果我的 XML 正确,将会帮助很多。

【问题讨论】:

  • 您是否包含 main.xml 中的 stuff.xml?例如curious-creature.org/2009/02/25/…
  • 如果您的意思是“您是否在 main.xml 中包含对 stuff.xml 的引用”?没有。就这么简单?
  • "是的,我确实 setContentView(R.layout.main); 就在 onCreate 之后。"您的意思是在调用 super.onCreate 之后,在您的 onCreate 方法中执行此操作吗?
  • 是的,我愿意: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);设置内容视图(R.layout.main); // setup DB infra 我之前的 ListView 工作正常;正是当我在一个单独的 XML 文件中添加了从属的 RelativeView 时,它的连接才能访问 id,这才搞砸了。

标签: android listview nested textview


【解决方案1】:

在您膨胀布局的地方,您可以在您膨胀的布局上使用 findViewById,因为我不知道您的代码是什么样的,这里是我要做的一个示例:

 LayoutInflater li = LayoutInflater.from(mContext);
 LinearLayout mLayout = (LinearLayout) li.inflate(R.layout.stuff,null);
 View mView = mLayout.findViewById(R.id.firstLine);

您可能希望您的 null 成为您希望膨胀布局拥有的父级。希望这会有所帮助!

更新

我会尝试替换你所拥有的:

thingView= new LinearLayout(getContext());
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View nuView = vi.inflate(resource, thingView, true);

与:

LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thingView = vi.inflate(R.layout.thing_item, null);

您似乎无缘无故地创建了一个新的线性布局,因为您的通货膨胀已经从您的 xml 文件中获得了线性布局。这几乎就是我上面所说的,这对我有用:)

【讨论】:

  • 是的,我已经在那里进行了充气,​​虽然做得有点不同。所有的调用都在工作(或者看起来如此),即:mLayout 返回一个值。它只是返回 null 的调用 findViewById。所以在你上面的例子中,mView == null。
  • 不;我将其归结为基本要素,因此我可以发布代码。我认为如果没有一些具体的代码,人们很难提供帮助。
  • Stealthcopter - 终于 - 解决了我在 XML 中的一个错字,整合了您的建议,然后确保将宽度和高度更改为“wrap_content”,成功了!谢谢!所以我很有信心你的建议解决了我原来的问题。不触发 getView() 的问题是我的 XML 指示“fill_parent”。其他 - 请参阅此链接! androidforums.com/android-developers/…
猜你喜欢
  • 1970-01-01
  • 2014-10-27
  • 2012-04-26
  • 2010-10-14
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
相关资源
最近更新 更多