我正在设计一个用户界面,其中我的应用程序有3个选项卡,我使用片段创建它,第三个选项卡我需要显示带有文本和图像的列表视图。我可以显示列表但是当我为每个选项写入点击监听器时项目应用程序开始崩溃,现在我有一个错误,无法findViewById()函数。我将与此共享我的代码。请将我解决 的 Contact.java
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.tutorialsbuzz.slidetablistview.R;
public class Contact extends ListFragment implements AdapterView.OnItemClickListener {
String[] member_names;
TypedArray profile_pics;
String[] statues;
String[] contactType;
List<RowItem> rowItems;
ListView mylistview;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.contact, container, false);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
rowItems = new ArrayList<RowItem>();
member_names = getResources().getStringArray(R.array.Member_names);
profile_pics = getResources().obtainTypedArray(R.array.profile_pics);
statues = getResources().getStringArray(R.array.statues);
contactType = getResources().getStringArray(R.array.contactType);
for (int i = 0; i < member_names.length; i++) {
RowItem item = new RowItem(member_names[i],
profile_pics.getResourceId(i, -1), statues[i],
contactType[i]);
rowItems.add(item);
}
mylistview= (ListView) findViewById(android.R.id.list);
CustomAdapter adapter = new CustomAdapter(getActivity(), rowItems);
setListAdapter(adapter);
profile_pics.recycle();
mylistview.setOnItemClickListener(this);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
setHasOptionsMenu(true);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), ummy.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(),ummy.class);
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), ummy.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(view.getContext(), ummy.class);
startActivityForResult(myIntent, 0);
}
if (position == 4) {
Intent myIntent = new Intent(view.getContext(), ummy.class );
startActivityForResult(myIntent, 0);
}
}
}
CustomAdapter.java
import java.util.List;
import com.tutorialsbuzz.slidetablistview.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
CustomAdapter(Context context, List<RowItem> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
ImageView profile_pic;
TextView member_name;
TextView status;
TextView contactType;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.member_name = (TextView) convertView
.findViewById(R.id.member_name);
holder.profile_pic = (ImageView) convertView
.findViewById(R.id.profile_pic);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.contactType = (TextView) convertView
.findViewById(R.id.contact_type);
RowItem row_pos = rowItems.get(position);
holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
holder.member_name.setText(row_pos.getMember_name());
holder.status.setText(row_pos.getStatus());
holder.contactType.setText(row_pos.getContactType());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
Contact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout `xmlns:android="http://schemas.android.com/apk/res/android"`
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
答案 0 :(得分:3)
使用getView()
来抓取您的观点。此方法返回片段的根视图。有了这个,你可以致电findViewById()
。或者执行以下操作
ListView mylistview;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.contact, container, false);
mylistview = (ListView) v.findViewById(android.R.id.list);
return v;
}
getView() method
: - 获取片段布局的根视图(onCreateView(LayoutInflater,ViewGroup,Bundle)返回的布局),如果提供的话。
使用getView()
内的onActivityCreated
方法查找您的观点,如下所示
myListVIew = (ListView) getView().findViewById(R.id.list);
答案 1 :(得分:0)
将适配器中的getView mehtod更改为
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.member_name = (TextView) convertView
.findViewById(R.id.member_name);
holder.profile_pic = (ImageView) convertView
.findViewById(R.id.profile_pic);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.contactType = (TextView) convertView
.findViewById(R.id.contact_type);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
RowItem row_pos = rowItems.get(position);
holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
holder.member_name.setText(row_pos.getMember_name());
holder.status.setText(row_pos.getStatus());
holder.contactType.setText(row_pos.getContactType());
return convertView;
}