gpt4 book ai didi

android - 应用程序因适配器而崩溃

转载 作者:行者123 更新时间:2023-11-30 03:42:26 26 4
gpt4 key购买 nike

自上周以来,我在 Listview 中显示 facebook 好友列表数据时遇到问题,我无法弄清楚。我尝试了很多方法来做到这一点,但不幸的是它没有成功。好友列表显示在 logcat 中,但是当我想在 ListView 中显示它时,应用程序崩溃了。出现空指针异常错误和其他一些运行时错误

请帮我解决这个问题
我只想在for循环中的ListView中显示好友信息 下面是我的代码

   public class About extends  Activity {
public ArrayList<String> values;

ListView listViewPhoneBook;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone_book);
values = new ArrayList<String>();
getfriends();
// Log.d("All Data",values.toString());
ListView listViewPhoneBook=(ListView)findViewById(R.id.listPhoneBook);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.each_contact, values);
listViewPhoneBook.setAdapter(adapter);
adapter.notifyDataSetChanged();}

public void getfriends(){
Request.executeMyFriendsRequestAsync( (Session)MainActivity.fbSession , new Request.GraphUserListCallback() {


@Override
public void onCompleted(List<GraphUser> users, Response response) {
for (int i = 0; i < users.size(); i++) {
values.add(users.get(i).getName());
}
Log.d("All Data1",values.toString());
}
});
}
}

日志

   03-20 14:18:28.156: E/ArrayAdapter(21885): You must supply a resource ID for a TextView
03-20 14:18:28.169: E/AndroidRuntime(21885): FATAL EXCEPTION: main
03-20 14:18:28.169: E/AndroidRuntime(21885): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.AbsListView.obtainView(AbsListView.java:2088)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.ListView.measureHeightOfChildren(ListView.java:1249)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.ListView.onMeasure(ListView.java:1160)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.view.View.measure(View.java:12773)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1374)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1022)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.view.View.measure(View.java:12773)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.view.View.measure(View.java:12773)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.LinearLayout.measureVertical(LinearLayout.java:815)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.view.View.measure(View.java:12773)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-20 14:18:28.169: E/AndroidRuntime(21885): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2108)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.view.View.measure(View.java:12773)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1147)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2642)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.os.Looper.loop(Looper.java:137)
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.app.ActivityThread.main(ActivityThread.java:4666)
03-20 14:18:28.169: E/AndroidRuntime(21885): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 14:18:28.169: E/AndroidRuntime(21885): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 14:18:28.169: E/AndroidRuntime(21885): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
03-20 14:18:28.169: E/AndroidRuntime(21885): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
03-20 14:18:28.169: E/AndroidRuntime(21885): at dalvik.system.NativeStart.main(Native Method)
03-20 14:18:28.169: E/AndroidRuntime(21885): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
03-20 14:18:28.169: E/AndroidRuntime(21885): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
03-20 14:18:28.169: E/AndroidRuntime(21885): ... 30 more

最佳答案

values = new ArrayList<String>();
getfriends();

for (int i = 0; i < users.size(); i++) {
values.add(users.get(i).getName());
Log.d("FRIENDSNAME", users.get(i).getName());
}

编辑:最终代码

使用此代码

public class About extends  Activity {

/*private String[] values;*/
public ArrayList<String> values;
//private ActivityList listAdapter;

ListView listViewPhoneBook;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub

super.onCreate(savedInstanceState);
setContentView(R.layout.phone_book);
values = new ArrayList<String>();
listViewPhoneBook=(ListView)findViewById(R.id.listPhoneBook);
getfriends();
// Log.d("All Data",values.toString());

}


public void updateList()
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.each_contact, values);
listViewPhoneBook.setAdapter(adapter);
adapter.notifyDataSetChanged();
}


public void getfriends()
{
Request.executeMyFriendsRequestAsync( (Session)MainActivity.fbSession , new Request.GraphUserListCallback() {


@Override
public void onCompleted(List<GraphUser> users, Response response) {
/*values = new String[users.size()];*/

for (int i = 0; i < users.size(); i++) {
// values[i] = users.get(i).getName();
values.add(users.get(i).getName());
/* Log.d("FRIENDSNAME", users.get(i).getName());*/

}
Log.d("All Data1",values.toString());
updateList();
}
});
}

关于android - 应用程序因适配器而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15518337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com