gpt4 book ai didi

java - Android 应用程序崩溃。不知道为什么

转载 作者:行者123 更新时间:2023-12-01 17:16:40 25 4
gpt4 key购买 nike

如果这个错误/问题太微不足道,我深表歉意。我开发 Android 应用程序的经验为零,所以我正在尝试经历 codepath 之一教程,特别是 this one ,我在幻灯片 11。

这是java:

包 com.example.simpletodo;

package com.example.simpletodo;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TodoActivity extends Activity {
ArrayList<String> items;
ArrayAdapter<String> itemsAdapter;
ListView lvItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
lvItems = (ListView) findViewById(R.id.lvItems);
items.add("First Item");
items.add("Second Item");
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.todo, menu);
return true;
}

}

这是 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TodoActivity" >

<ListView
android:id="@+id/lvItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/etNewITem"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >

</ListView>

<EditText
android:id="@+id/etNewITem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lvItems"
android:layout_alignParentBottom="true"
android:ems="10"
android:hint="Enter new item" >

<requestFocus />
</EditText>

<Button
android:id="@+id/btnAddItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Add" />

</RelativeLayout>

应用程序似乎在 java 第 22 行崩溃 lvItems.setAdapter(itemsAdapter);

如有任何建议,我们将不胜感激。我是一个 php 人,所以我不习惯冗长的代码(学习)以及如何调试已编译的程序。谢谢!

来自 logcat 的错误消息:

02-06 16:29:31.563: W/dalvikvm(18897): threadid=1: thread exiting with uncaught exception (group=0x41d39700)
02-06 16:29:31.563: E/AndroidRuntime(18897): FATAL EXCEPTION: main
02-06 16:29:31.563: E/AndroidRuntime(18897): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpletodo/com.example.simpletodo.TodoActivity}: java.lang.NullPointerException
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.access$700(ActivityThread.java:168)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.os.Looper.loop(Looper.java:137)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.main(ActivityThread.java:5493)
02-06 16:29:31.563: E/AndroidRuntime(18897): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 16:29:31.563: E/AndroidRuntime(18897): at java.lang.reflect.Method.invoke(Method.java:525)
02-06 16:29:31.563: E/AndroidRuntime(18897): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
02-06 16:29:31.563: E/AndroidRuntime(18897): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
02-06 16:29:31.563: E/AndroidRuntime(18897): at dalvik.system.NativeStart.main(Native Method)
02-06 16:29:31.563: E/AndroidRuntime(18897): Caused by: java.lang.NullPointerException
02-06 16:29:31.563: E/AndroidRuntime(18897): at com.example.simpletodo.TodoActivity.onCreate(TodoActivity.java:21)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.Activity.performCreate(Activity.java:5372)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
02-06 16:29:31.563: E/AndroidRuntime(18897): ... 11 more

最佳答案

您永远不会初始化 items 变量,这会导致您在尝试设置适配器时出现空指针异常,因为 items 为 null。

这样做:

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
// initialize
items = new ArrayList<String>();
// add content
items.add("First Item");
items.add("Second Item");

lvItems = (ListView) findViewById(R.id.lvItems);
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
}

您可以在 logcat 中看到错误:

...
Caused by: java.lang.NullPointerException 02-06 16:26:55.038: E/AndroidRuntime(17822): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330) 02-06 16:26:55.038: E/AndroidRuntime(17822): at android.widget.ListView.setAdapter(ListView.java:470) 02-06 16:26:55.038: E/AndroidRuntime(17822): at com.example.simpletodo.TodoActivity.onCreate(TodoActivity.java:22) 02-06 ....

这告诉您在 TodoActivity 第 22 行发生了 NullPointerException

关于java - Android 应用程序崩溃。不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21615329/

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