gpt4 book ai didi

Android - 更新列表适配器会创建 NullPointErexception

转载 作者:行者123 更新时间:2023-11-29 22:09:15 25 4
gpt4 key购买 nike

我有一个 ListActivity,它有一个崩溃的适配器。这是使程序崩溃的代码:

Question q = new Question ();
q.setQuestion( "" );

questions.add(q);

adapter = new ArrayAdapter<Question>(this, R.layout.user_question_list,
R.id.label, questions);

这是我创建适配器的方式:

ArrayAdapter<Question> adapter; 

ArrayList<Question> questions = new ArrayList <Question>( );

列表 xml 是这样的:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>

而整体的Activity xml是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >


<TextView
android:id="@+id/question_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading your questions..."
/>

<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</ListView>

<Button
android:id="@+id/add_question"
android:layout_height="wrap_content"
android:text="Ask a Question"
android:onClick="sendFeedback"
android:layout_width="fill_parent">
</Button>

</LinearLayout>

知道为什么我尝试更新适配器的代码会导致 NullPointerException 吗?

错误日志如下:

04-06 18:43:19.626: D/AndroidRuntime(2564): Shutting down VM
04-06 18:43:19.626: W/dalvikvm(2564): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-06 18:43:19.686: E/AndroidRuntime(2564): FATAL EXCEPTION: main
04-06 18:43:19.686: E/AndroidRuntime(2564): java.lang.NullPointerException
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.ListView.onMeasure(ListView.java:1155)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.view.View.measure(View.java:12723)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.view.View.measure(View.java:12723)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.view.View.measure(View.java:12723)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.view.View.measure(View.java:12723)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-06 18:43:19.686: E/AndroidRuntime(2564): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.view.View.measure(View.java:12723)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.os.Looper.loop(Looper.java:137)
04-06 18:43:19.686: E/AndroidRuntime(2564): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-06 18:43:19.686: E/AndroidRuntime(2564): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 18:43:19.686: E/AndroidRuntime(2564): at java.lang.reflect.Method.invoke(Method.java:511)
04-06 18:43:19.686: E/AndroidRuntime(2564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-06 18:43:19.686: E/AndroidRuntime(2564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-06 18:43:19.686: E/AndroidRuntime(2564): at dalvik.system.NativeStart.main(Native Method)

这是 Activity 开始的样子:

public class MyQuestionsActivity extends ListActivity
{
ArrayAdapter<Question> adapter;

ArrayList<Question> questions = new ArrayList <Question>( );

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.users_questions);

// Make sure the user is logged in
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( MyQuestionsActivity.this);
final String user_id = prefs.getString( "user_id" , null );

// If the user is not logged in, send them to log in
if ( user_id == null )
{
sendEmail("My Questions Error", "User id is empty but they chose the my-questions button. They should be logged in before they see this button. Here is their user_id: " + user_id );

Intent loginIntent = new Intent( MyQuestionsActivity.this, LoginActivity.class);
MyQuestionsActivity.this.startActivity(loginIntent);
}

final TextView question_label = (TextView) findViewById(R.id.question_label);

Question q = new Question ();
q.setQuestion( "" );

questions.add(q);

adapter = new ArrayAdapter<Question>(this, R.layout.user_question_list,
R.id.label, questions);

setListAdapter ( adapter );

ListView lv = getListView();
lv.setTextFilterEnabled(true);
...

以下是问题类的字段:

public String questionId;
public String question;
public ArrayList <QuestionComment> questionComments;
public String questionByMemberId;
public String authorName;

谢谢!!

最佳答案

试试这个,

adapter = new ArrayAdapter<Question>(this, R.layout.user_question_list, questions);

让我知道发生了什么..

关于Android - 更新列表适配器会创建 NullPointErexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10052039/

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