gpt4 book ai didi

java - Android 自定义 ArrayAdapter - 崩溃

转载 作者:行者123 更新时间:2023-12-01 13:44:54 25 4
gpt4 key购买 nike

我必须使用 CustomItems 创建一个 ListView,就像本教程中所示:http://www.thepcwizard.in/2012/09/android-creating-custom-listview-for.html

但是如果我尝试执行该应用程序,它就会崩溃。

这里是 Logcat 消息:

12-06 12:30:18.034    1631-1631/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a91b90)
12-06 12:30:18.084 1631-1631/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.test.testapp, PID: 1631
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testapp/com.test.testapp.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.test.testapp.MainActivity.onCreate(MainActivity.java:62)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.access$700(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)

有什么想法吗?我的代码与教程中显示的几乎相同。

感谢您的帮助。

编辑:这是我的 OnCreate() 方法

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}


ListView msgList;
ArrayList<BuildItem> details;
AdapterView.AdapterContextMenuInfo info;
msgList = (ListView) findViewById(R.id.listViewBuilds);

details = new ArrayList<BuildItem>();

BuildItem Detail;
Detail = new BuildItem();
Detail.setBuildName("TestBuil1");
Detail.setUserdName("TestUser1");
details.add(Detail);

BuildItem Detail2;
Detail2 = new BuildItem();
Detail2.setBuildName("TestBuil2");
Detail2.setUserdName("TestUser2");
details.add(Detail2);

BuildItem Detail3;
Detail3 = new BuildItem();
Detail3.setBuildName("TestBuil3");
Detail3.setUserdName("TestUser3");
details.add(Detail3);


msgList.setAdapter(new CustomAdapter(details , this)); //Line 62

}

这里是activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.testapp.MainActivity"
tools:ignore="MergeRootFrame" />

fragment_main.xml(其中包含 ListView“listViewBuilds”)

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.test.testapp.MainActivity$PlaceholderFragment">


<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewBuilds"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp" />

list_item_build.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:background="#b9d4ff"
android:layout_height="100px"
android:orientation="horizontal"
android:weightSum="1">

<ImageView
android:layout_width="75px"
android:layout_height="75px"
android:id="@+id/imageViewIcon"
android:layout_marginTop="12.5px"
android:layout_marginLeft="12px"
android:background="#054f74" />

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="12dp"
android:layout_marginTop="0dp"
android:layout_marginRight="5dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textViewBuild"
android:layout_marginTop="5dp"
android:text="Build" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Username"
android:id="@+id/textViewUser"
android:layout_marginLeft="25px" />
</LinearLayout>

</LinearLayout>

</LinearLayout>

最佳答案

您的布局activity_main不包含ID为listViewBuildsListView,并且findViewById()返回。尝试对 null 引用调用 setAdapter() 会导致 NullPointerException

关于java - Android 自定义 ArrayAdapter - 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20430226/

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