java.lang.NullPointerException : storage == null 异常

标签 java android

之前的错误是“你的内容必须有一个 ListView ,它的 id 属性是“Android.R.id.list”我做了一些谷歌搜索,结果是这个

Caused by: java.lang.NullPointerException: storage == null
at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:155)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at edu.drexel.weatherundergroundapi.CustomListAdapter.<init>(CustomListAdapter.java:35)
at edu.drexel.weatherundergroundapi.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)



      

这些是 CustomListActivity 中的代码。

public CustomListAdapter(Activity context, String[] time, String[] condition, String[] temp, String[] humidity, String[] url)
{
    super(context, R.layout.listview, time);
    this.context = context;
    this.time = time;
    this.condition = condition;
    this.temp = temp;
    this.humidity = humidity;
    this.url = url;
}

这是mainActivity中的代码

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

    MyBgTask asyncTask = new MyBgTask();

    CustomListAdapter adapter = new CustomListAdapter(this, asyncTask.getTimeArray(), asyncTask.getConditionArray(),
                                asyncTask.getTempArray(), asyncTask.getHumidityArray(), asyncTask.getUrls());
    setListAdapter(adapter);

    asyncTask.execute();
}

这是activity_main.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: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=".main">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:clickable="false"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" >


</ListView>

这是listview.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">

<ImageView
    android:id="@+id/icon"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:padding="5dp"
    android:contentDescription="nothing" />

<LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dateTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:padding="2dp"
        android:textColor="#33CC33"
        android:text="@string/dateTime" />
    <TextView
        android:id="@+id/condition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/condition"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:id="@+id/temp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/temp"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/humidity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/humidity"
        android:layout_marginLeft="10dp" />
</LinearLayout>

最佳答案

我想你想从 asyncTask 中获取适配器数据,但是,当你设置 ListAdapter 时,asyncTask 还没有启动。所以它将为空。

在 MyBgTask.onPostExecute() 方法中执行 setListAdapter 会有所帮助;

关于java.lang.NullPointerException : storage == null 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28802112/

相关文章:

java - recyclerview inside nestedscrollview addOnScrollListener (endless scrolllistener)

android - @Parcelize 注解和继承

android - Unresolved reference :尽管实现了支持,但RecyclerView:appcompat-v7:28.0.0

java - 无法从 Firebase 获取用户名列表

android - 如何知道哪个 fragment 称为另一个 fragment ?

java - 如何在不使用 "if-else if"的情况下编写对不同参数使用不同方法的代码?

java - 如何从 Maven 命令行运行 testng.xml

关于类加载器的java类标识

java - 如何将 WebView 的内容居中?

java - ActiveMQ ProducerFlowControl 如何用于异步主题?