Android 尝试查找嵌套TextView(在ListView中)的DviewById时失败

Android 尝试查找嵌套TextView(在ListView中)的DviewById时失败,android,listview,nested,textview,Android,Listview,Nested,Textview,正在尝试使用由两个文本视图组成的行的ListView(可滚动列表)。我有一个数据库中的项目列表,我想填充到LinearLayout->ListView->TextView中,但无法获取id 布局有点像这个指导性链接,但已经从RelativeLayout中退出,并使用LinearLayout使其工作。甚至还不担心它的样子;只是还不能把它连接起来 有两个XML文件(下面非常简短的细节) main.xml和stuff.xml main.xml具有 …文本视图 …列表视图 stuff.xml已经

正在尝试使用由两个文本视图组成的行的ListView(可滚动列表)。我有一个数据库中的项目列表,我想填充到LinearLayout->ListView->TextView中,但无法获取id

布局有点像这个指导性链接,但已经从RelativeLayout中退出,并使用LinearLayout使其工作。甚至还不担心它的样子;只是还不能把它连接起来

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

main.xml具有

…文本视图

…列表视图

stuff.xml已经

... android:id=“@+id/firstLine”

。。。 android:id=“@+id/secondLine”

是的,我不设置ContentView(R.layout.main);就在onCreate之后

在findViewByID(第一行)和FindViewWid(第二行)上获取null

我有一个阵列适配器,可以在其中为stuffView充气。我对其他示例的思考和理解是,在我有意膨胀它之前,它不会膨胀(这个嵌套的stuffView)。这一切都很好,但当我执行findViewById时,它返回null,因此无法设置text()

史诗般的失败是因为我完全无知/无知。注意:我已经仔细阅读了雷托的书,特别是163页的一个类似的例子,但是失败了

善良的灵魂能给我指明正确的方向吗

我必须膨胀这个嵌套视图吗?(雷托的例子确实如此)。如果是,我错过了什么?我希望有人能给我举个更好的例子。在这一点上,我的代码可能太复杂,无法发布,而且有点专有

谢谢

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-非常简单)

主类:showlayoutwelly.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();

}
}
package com.blap.test;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.os.Bundle;
导入android.widget.ListView;
导入android.widget.TextView;
公共类showlayout很好地扩展了活动{
列表视图;
文本视图myTextView;
ArrayList thingys;
适应aa的东西;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListView=(ListView)findViewById(R.id.myListView);
myTextView=(TextView)findViewById(R.id.identText);
thingys=新的ArrayList();
aa=新的ThingyaAdapter(这个,android.R.layout.simple\u list\u item\u 1,thingys);
myListView.setAdapter(aa);
//real deal有一个从数据库中查找项目、填充数组和通知更改的调用。
Thingy thing1=新的Thingy(“第一”、“第一行”);
添加(第1项);
//遗憾的是,这并没有触发我已经覆盖的getView()。。。
aa.notifyDataSetChanged();
Thingy thing2=新的Thingy(“第二行”、“第二行”);
添加(第2项);
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;
}
}
package com.blap.test;
导入java.util.List;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类ThingyAdapter扩展了ArrayAdapter{
智力资源;
公共内容适配器(上下文、上下文、int资源、列表项){
超级(\u上下文、\u资源、\u项目);
资源=_资源;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
线性布局thingView;
Thingy thingItem=getItem(位置);
String identString=thingItem.getIdent();
字符串nameString=thingItem.getName();
if(convertView==null){
thingView=newlinearLayout(getContext());
LayoutInflater vi=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
视图nuView=vi.inflate(资源,thingView,true);
}
否则{
thingView=(线性布局)convertView;
}
//问题是,这些调用返回空值。
TextView行1=(TextView)thingView.findViewById(R.id.firstLine);
TextView行2=(TextView)thingView.findViewById(R.id.secondLine);
//自然地,这些不光彩地呕吐。。。。
row1.setText(thingItem.getIdentit());
row2.setText(thingItem.getName());
返回thingView;
}
}

所以这段代码本质上就是我在寻求帮助的东西;把名字阉了叫它什么的。。。。此示例未触发getView()。这是我必须解决的第二个问题。更重要的是,您对findViewById失败的帮助,以及如果我正确使用了XML,将有很大帮助。

如果您正在膨胀布局,您可以在正在膨胀的布局上使用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);
与:


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

如果你正在膨胀你的布局,你可以在你正在膨胀的布局上使用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);
与:


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

您是否包括main.xml中的stuff.xml?e、 g.如果您的意思是“是否在main.xml中包含对stuff.xml的引用”?不。米格