尝试在空对象引用上调用虚拟方法'int android.view.View.getImportantForAccessibility()'

时间:2019-03-20 09:47:47

标签: java android

我试图在地震应用程序的Listview中显示数据,该数据显示地震的幅度和位置。我使用一个自定义适配器显示数据,但出现一条错误消息“尝试调用虚拟方法 在空对象引用上'int android.view.View.getImportantForAccessibility()'” 我正在共享代码。预先感谢。

主要活动

package com.example.quake_report;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public static final String LOG_TAG = MainActivity.class.getName();

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

        ArrayList<earthquake> earthquakes = new ArrayList<earthquake>();
        earthquakes.add(new earthquake("3.4","San Francisco"));
        earthquakes.add(new earthquake("2.1","Paris"));
        earthquakes.add(new earthquake("5.6","London"));
        earthquakes.add(new earthquake( "7.2","Tokyo"));
        earthquakes.add(new earthquake( "1.4","Mexico City"));
        earthquakes.add(new earthquake( "3.7","Moscow"));
        earthquakes.add(new earthquake( "6.2","Rio De Janerio"));
        earthquakes.add(new earthquake( "2.9","Japan"));
        earthquakes.add(new earthquake( "5.1","Australia"));


        ListView earthquakeListView =(ListView) findViewById(R.id.list);

        earthquakeAdapter adapter = new earthquakeAdapter(this, earthquakes);

        earthquakeListView.setAdapter(adapter);

    }
}

earthquake.java

package com.example.quake_report;

public class earthquake {
    private String _magnitude;
    private String _location;


    public earthquake(String magnitude,String location) {
        _magnitude = magnitude;
        _location = location;

    }

    public String get_magnitude(){
        return _magnitude;
    }

    public String get_location(){
        return _location;
    }

}

earthquakeAdapter.java

package com.example.quake_report;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;

public class earthquakeAdapter extends ArrayAdapter<earthquake> {

    private static final String LOG_TAG = earthquakeAdapter.class.getSimpleName();

    public earthquakeAdapter(Context context, ArrayList<earthquake> earthquakes) {
        super(context,0, earthquakes);
    }

    @Override
    public View getView(int position,  View convertView, ViewGroup parent) {
        LayoutInflater earthquake = LayoutInflater.from(getContext());
        View customView = convertView;
        if(customView == null){
            customView = earthquake.inflate(R.layout.custom_layout,parent,false);

        }
        earthquake currentEarthquakeAdapter = getItem(position);
        TextView Magnitude = (TextView) customView.findViewById(R.id.magnitude);
        Magnitude.setText(currentEarthquakeAdapter.get_magnitude());
        TextView Location = (TextView) customView.findViewById(R.id.location);
        Location.setText(currentEarthquakeAdapter.get_location());

        return convertView;
    }
}

activity_main.xml

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

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10sp"
    android:paddingLeft="10sp"
    android:paddingRight="10sp">

    <TextView
        android:id="@+id/magnitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textStyle="bold" />


</LinearLayout>

错误消息

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.quake_report, PID: 1059
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
        at android.widget.AbsListView.obtainView(AbsListView.java:2389)
        at android.widget.ListView.makeAndAddView(ListView.java:2052)
        at android.widget.ListView.fillDown(ListView.java:786)
        at android.widget.ListView.fillFromTop(ListView.java:847)
        at android.widget.ListView.layoutChildren(ListView.java:1826)
        at android.widget.AbsListView.onLayout(AbsListView.java:2171)
        at android.view.View.layout(View.java:19590)
        at android.view.ViewGroup.layout(ViewGroup.java:6053)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:19590)
        at android.view.ViewGroup.layout(ViewGroup.java:6053)
        at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
        at android.view.View.layout(View.java:19590)
        at android.view.ViewGroup.layout(ViewGroup.java:6053)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:19590)
        at android.view.ViewGroup.layout(ViewGroup.java:6053)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:19590)
        at android.view.ViewGroup.layout(ViewGroup.java:6053)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:758)
        at android.view.View.layout(View.java:19590)
        at android.view.ViewGroup.layout(ViewGroup.java:6053)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2200)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Application terminated.

1 个答案:

答案 0 :(得分:0)

您好Naresh Negi您只需从地震适配器中删除一行

@Override
    public View getView(int position,  View convertView, ViewGroup parent) {
        LayoutInflater earthquake = LayoutInflater.from(getContext());
//        View customView = convertView;
        if(convertView == null){
            convertView = earthquake.inflate(R.layout.custom_layout,parent,false);

        }
        earthquake currentEarthquakeAdapter = getItem(position);
        TextView Magnitude = (TextView) convertView.findViewById(R.id.magnitude);
        Magnitude.setText(currentEarthquakeAdapter.get_magnitude());
        TextView Location = (TextView) convertView.findViewById(R.id.location);
        Location.setText(currentEarthquakeAdapter.get_location());

        return convertView;
    }

使用convertView而不是每次都分配。我们只是在第一次分配之后,它将在适配器

中保存视图的引用