繁体   English   中英

NullPointerException - 尝试调用虚拟方法 `notifyDataSetChanged()` - 看不到创建的列表

[英]NullPointerException - attempt to invoke virtual method `notifyDataSetChanged()` - can't see list created

我正在按照本教程的方式工作,并且一直在向列表添加名称并填充列表。

这是我想要得到的,我在输入框中输入的名称列表: http://www.raywenderlich.com/78576/android-tutorial-for-beginners-part-2

(带下划线的“Darryl”是输入框,它在顶部显示该名称(点击按钮后),然后将其添加到列表中)。

虽然我收到此错误:

08-24 18:17:50.903  21935-21935/com.example.batman.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.batman.myapplication, PID: 21935
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.notifyDataSetChanged()' on a null object reference
            at com.example.batman.myapplication.MainActivity.onClick(MainActivity.java:65)
            at android.view.View.performClick(View.java:5217)
            at android.view.View$PerformClick.run(View.java:20983)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6141)
            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:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

这是我的MainActivity.java

package com.example.batman.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView mainTextView;
    EditText mainEditText;
    ListView mainListView;
    ArrayAdapter mArrayAdapter;
//  ArrayList<String> mNameList = new ArrayList<String>();
    ArrayList mNameList = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 1. Access the TextView defined in layout XML
        // and then set its text
        mainTextView = (TextView) findViewById(R.id.main_textview);
        mainTextView.setText("Set in Java!");

        Button mainButton;
        mainButton = (Button) findViewById(R.id.main_button);
        mainButton.setOnClickListener(this);

        // 3.  Access the EditText defined in layout XML
        mainEditText = (EditText) findViewById(R.id.main_edittext);

        // 4. Access the ListView
        mainListView = (ListView) findViewById(R.id.main_listview);
        // Create an ArrayAdapter for the ListView
        mArrayAdapter = new ArrayAdapter(this,  // removed ArrayAdapter before mArrayAdapter
                android.R.layout.simple_list_item_1,
                mNameList);
        // Set the ListView to use the ArrayAdapter
        mainListView.setAdapter(mArrayAdapter);
    }

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

    @Override
    public void onClick(View v) {
        // Take what was typed into the EditText
        // and use in TextView
        mainTextView.setText(mainEditText.getText().toString() + ".");

        // Also add that value to the list shown in the ListView
        mNameList.add(mainEditText.getText().toString());
        mArrayAdapter.notifyDataSetChanged();
    }
} // end class

这是ActivityMain.XML

<LinearLayout 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:orientation="vertical"
              tools:context=".MainActivity">

    <TextView
        android:id="@+id/main_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="textView"/>
    <!-- Displays keyboard when touched -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- List whose dataset is defined in code with an adapter -->
        <ListView
            android:id="@+id/main_listview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_marginTop="20dp"/>

        <!-- Set OnClickListener to trigger results when pressed -->

        <Button
            android:id="@+id/main_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:text="button" />
        <!-- Shows an image from your drawable resources -->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:src="@drawable/ic_launcher" />
        <!-- Closing tag for the horizontal nested layout -->
    </LinearLayout>

    <EditText
        android:id="@+id/main_edittext"
        android:inputType="textCapSentences"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:hint="hint"/>

    <!-- Closing tag for the horizontal nested layout -->
</LinearLayout>

我还在new ArrayAdapter(this, android.R.layout.simple_list_item_1, mNameList);上得到了一些突出显示(Android Studio new ArrayAdapter(this, android.R.layout.simple_list_item_1, mNameList); 行,说有一个“未经检查的电话”。

正如您在我的一条评论中看到的那样,我是否需要通过诸如ArrayList<String> mNameList = new ArrayList<String>();类的东西使mNameList成为字符串ArrayList<String> mNameList = new ArrayList<String>(); ?

(如果您需要清单或我的MainActivity.xml ,请告诉我)。

感谢您的任何想法!

编辑:感谢评论,我从 onClick 部分删除了ArrayAdapter 问题的最后一部分是获取要显示的列表。 它现在显示输入,但不会显示在下面的列表中。 我也附上了 XML。

你没有初始化全局变量 mArrayAdapter

改变 :

ArrayAdapter mArrayAdapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1,
            mNameList);

到 :

mArrayAdapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1,
            mNameList);

在您的活动的 onCreate 方法中

您的 mArrayAdapter 始终为空,因为您正在使用以下内容创建本地 mArrayAdapter:

ArrayAdapter mArrayAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1,
                mNameList);

所以......只需删除ArrayAdapter这样你就可以初始化适配器的全局版本,你应该没问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM