java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.View.getImportantForAccessibility()”

Posted

技术标签:

【中文标题】java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.View.getImportantForAccessibility()”【英文标题】:java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference 【发布时间】:2015-04-20 12:12:15 【问题描述】:

我正在尝试根据输入字段ArrayListListView 组合一个购物清单应用程序。该应用程序将基于Fragments。但是,我遇到了一个问题,不知道如何解决。

我在 Google 和 *** 上环顾四周,发现了一些信息。但是,我还没有让它工作。现在我希望我的代码可以得到帮助。

我在 android 开发方面相对缺乏经验。

Main.java

import java.util.ArrayList;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

public class Main extends FragmentActivity 
    
    ArrayList<Detail> items = new ArrayList<Detail>();
    FragmentManager FM;
    FragmentTransaction FT;
    Input input = new Input();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        replaceFragment(R.id.container, input);
    

    
    public void replaceFragment(int container, Fragment fragment)
        FM = getFragmentManager();
        FT = FM.beginTransaction();
        
        FT.replace(container, fragment);
        FT.commit();
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) 
            return true;
        
        return super.onOptionsItemSelected(item);
    

ListAdapter.java

public class ListAdapter extends BaseAdapter 

    Context context;
    ArrayList<Detail> items;
    
    public ListAdapter(Context context, ArrayList<Detail> items) 
        this.context = context;
        this.items = items;
    
    
    @Override
    public int getCount() 
        return items.size();
    

    @Override
    public Object getItem(int position) 
        return items.get(position);
    

    @Override
    public long getItemId(int position) 
        return items.indexOf(getItem(position));
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
        
        if(convertView == null)
            LayoutInflater lInflater = (LayoutInflater)context.getSystemService(
                    Activity.LAYOUT_INFLATER_SERVICE);
            
            convertView = lInflater.inflate(R.layout.list_row, null);       
        
        
        TextView title = (TextView)convertView.findViewById(R.id.itemTitle);
        CheckBox done = (CheckBox)convertView.findViewById(R.id.itemDone);
        
        
        Detail detail_position = items.get(position);
        
        title.setText(detail_position.getTitle());
        done.setChecked(detail_position.isDone());
        
        return null;
    

List.java

import java.util.ArrayList;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class List extends ListFragment implements OnItemClickListener

    ArrayList<Detail> items;
    ListAdapter adapter;
    private onClickListener OCL;

    interface onClickListener
        public void onItemClick(int id);
    

    public List(ArrayList<Detail> items) 
        this.items = items;
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 

        View root = inflater.inflate(R.layout.list_layout, container, false);
        return root;
    

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
        super.onActivityCreated(savedInstanceState);

        if(items != null)
            adapter = new ListAdapter(getActivity(), items);
            setListAdapter(adapter);
            getListView().setOnItemClickListener(this);
        

    

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) 

        if(OCL != null)
            OCL.onItemClick(position);
               
    

list_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_
    android:layout_
    android:orientation="vertical" >
    <ListView
        android:id="@+id/android:list"
        android:layout_
        android:layout_ >
    </ListView>
</LinearLayout>

list_row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="horizontal"
    android:padding="10dp" >
    <TextView
        android:id="@+id/itemTitle"
        android:layout_
        android:layout_
        android:layout_weight="0.96"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <CheckBox
        android:id="@+id/itemDone"
        android:layout_
        android:layout_
        android:text="Done" />
</LinearLayout>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_
    android:layout_
    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="com.example.com.hermods.lab2_2.Main" >
</RelativeLayout>

日志

02-19 09:10:47.421: E/AndroidRuntime(12745): Process: com.example.com.hermods.lab2_2, PID: 12745
02-19 09:10:47.421: E/AndroidRuntime(12745): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.AbsListView.obtainView(AbsListView.java:2360)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.ListView.makeAndAddView(ListView.java:1864)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.ListView.fillDown(ListView.java:698)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.ListView.fillFromTop(ListView.java:759)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.ListView.layoutChildren(ListView.java:1673)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.AbsListView.onLayout(AbsListView.java:2148)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.View.layout(View.java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.ViewGroup.layout(ViewGroup.java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.View.layout(View.java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.ViewGroup.layout(ViewGroup.java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.View.layout(View.java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.ViewGroup.layout(ViewGroup.java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.View.layout(View.java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.ViewGroup.layout(ViewGroup.java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.View.layout(View.java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.ViewGroup.layout(ViewGroup.java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.View.layout(View.java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.ViewGroup.layout(ViewGroup.java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.Choreographer.doFrame(Choreographer.java:550)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.os.Handler.handleCallback(Handler.java:739)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.os.Looper.loop(Looper.java:135)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at android.app.ActivityThread.main(ActivityThread.java:5221)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at java.lang.reflect.Method.invoke(Native Method)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at java.lang.reflect.Method.invoke(Method.java:372)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-19 09:10:47.421: E/AndroidRuntime(12745):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

【问题讨论】:

您知道return items.indexOf(getItem(position)); 在您的情况下等于position 吗?首先要尝试的是从List 中删除构造函数。 【参考方案1】:

在您的 public View getView 方法中将 return null; 更改为 return convertView;

【讨论】:

@user2734182 你能扩展你的答案并解释为什么convertView永远是null,它有什么特殊含义吗? @dark_ruby:返回 View 所需的适配器的 getView()。如果您返回 null,您将收到完全相同的错误消息。【参考方案2】:

#使用return convertView;

代码:

public View getView(final int position, View convertView, ViewGroup parent) 

    //convertView = null;

    if (convertView == null) 

        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.list_item, null);     

        TextView tv = (TextView) convertView.findViewById(R.id.name);
        Button rm_btn = (Button) convertView.findViewById(R.id.rm_btn);

        Model m = modelList.get(position);
        tv.setText(m.getName());

        // click listener for remove button  คลิกลบปุ่ม
        rm_btn.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                modelList.remove(position);
                notifyDataSetChanged();
            
        );
    

    ///#use    return convertView;
    return convertView;

【讨论】:

【参考方案3】:

在你的 baseadapterconstructor 中尝试初始化 LayoutInflater,通常我更喜欢这种方式,

public ClassBaseAdapter(Context context,ArrayList<Integer> listLoanAmount) 
    this.context = context;
    this.listLoanAmount = listLoanAmount;
    this.layoutInflater = LayoutInflater.from(context);

在类的顶部创建 LayoutInflater 变量,希望对您有所帮助

【讨论】:

【参考方案4】:

当我们在 fragment 的任何活动中使用自定义适配器时,有时会发生这种情况。我们返回空对象,即空视图,因此活动会混淆要加载哪个视图,这就是发生此异常的原因

shows the position where to change the view

【讨论】:

【参考方案5】:

我的愚蠢错误是: 将 != 更改为 ==

if(convertView != null)  // <---- HERE 
                LayoutInflater layoutInflater = LayoutInflater.from(z_selBoardElectricity.this);
                convertView = layoutInflater.inflate(R.layout.listview_board_alert, null);

                TextView textView = convertView.findViewById(R.id.board_name_tv);
                ImageView imageView = convertView.findViewById(R.id.board_imageview);

                textView.setText(text_list.get(position));
                imageView.setImageDrawable(imageAddressList.get(position));

                convertView.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        Intent intent = new Intent();
                        intent.putExtra("MESSAGE", text_list.get(pos));
                        setResult(98, intent);
                        finish();
                    
                );
            
return convertView;

【讨论】:

我今天又这样做了,我的错!

以上是关于java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.View.getImportantForAccessibility()”的主要内容,如果未能解决你的问题,请参考以下文章

亲測,Eclipse报&quot;An error has occurred,See error log for more details. java.lang.NullPointerExce

来自 LayerDrawable 的异常

Microsoft Access 和 Java JDBC-ODBC 错误

如何使用其id获取单个SQLite行数据?