我无法使用textinvite
访问我的Listfragment类的onActivityCreated()中的textview getListView().findViewById(R.id.invitetext)
。它返回null。我究竟做错了什么?我已经在SO上发了很多帖子,但答案对我不起作用。
PhoneContactsListFragment.java:
public class PhoneContactsListActivity extends FragmentActivity implements
PhoneContactsListFragment.OnContactsInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_contact_list);
...
}
activity_phone_contact_list.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/phone_contact_list"
android:name="com.example.proj.PhoneContactsListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
PhoneContactsListFragment.java:
public class PhoneContactsListFragment extends ListFragment implements
AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.contact_list_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(mAdapter);
if(getListView().findViewById(R.id.textinvite)==null)Log.d("Faizal","can't find invitetext");
...
}
private class ContactsAdapter extends CursorAdapter implements SectionIndexer {
...
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
final View itemLayout =
mInflater.inflate(R.layout.phone_contact_list_item, viewGroup, false);
...
return itemLayout;
}
contact_list_fragment.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/ContactListView"/>
<TextView android:id="@id/android:empty"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/no_contacts"
android:fontFamily="sans-serif-light"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</FrameLayout>
phone_contact_list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
style="@style/listViewActivatedStyle">
<TextView android:id="@android:id/text2"
android:paddingLeft="@dimen/listview_item_padding"
android:paddingRight="@dimen/listview_item_padding"
android:layout_width="wrap_content"
android:layout_height="26dp"
android:layout_toRightOf="@android:id/icon"
android:layout_alignParentBottom="true"
android:fontFamily="sans-serif"
android:singleLine="true"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView android:id="@android:id/text1"
android:paddingLeft="@dimen/listview_item_padding"
android:paddingRight="@dimen/listview_item_padding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@android:id/text2"
android:layout_toRightOf="@android:id/icon"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:fontFamily="sans-serif-light"
android:singleLine="true"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView android:id="@+id/textinvite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFF0000"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:fontFamily="sans-serif"
android:singleLine="true"
android:ellipsize="marquee"
android:text="@string/invite"
android:gravity="right"
android:layout_gravity="right"
android:visibility="gone"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</RelativeLayout>
答案 0 :(得分:1)
R.id.textinvite
R.layout phone_contact_list_item
内的不在R.layout.contact_list_fragment
内。
答案 1 :(得分:0)
以下是 TextView ,其中 textinvite 几乎位于 phone_contact_list_item 中..您已经尝试查找该TextView的ID在 contact_list_fragment ..这就是原因。
答案 2 :(得分:0)
试试这种方式
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.contact_list_fragment, container, false);
.....getListView().findViewById(R.id.textinvite).....
return rootView;
}