web-dev-qa-db-de.com

java.lang.NullPointerException: Versuch, die virtuelle Methode 'int Android.view.View.getImportantForAccessibility ()' für eine Nullobjektreferenz aufzurufen

Ich versuche, eine Einkaufslisten-App zusammenzustellen, basierend auf den Eingabefeldern ArrayList und ListView. Die App basiert auf Fragments. Ich bin jedoch auf ein Problem gestoßen und weiß nicht, wie ich es lösen soll.

Ich habe mich sowohl bei Google als auch bei StackOverflow umgesehen und einige Informationen gefunden. Ich habe es jedoch nicht zum Laufen gebracht. Nun hoffe ich, dass ich mit meinem Code Hilfe bekommen kann.

Ich bin in der Android-Entwicklung relativ unerfahren.

Danke im Voraus

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_width="match_parent"
    Android:layout_height="match_parent"
    Android:orientation="vertical" >
    <ListView
        Android:id="@+id/Android:list"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent" >
    </ListView>
</LinearLayout>

list_row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:orientation="horizontal"
    Android:padding="10dp" >
    <TextView
        Android:id="@+id/itemTitle"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_weight="0.96"
        Android:text="Medium Text"
        Android:textAppearance="?android:attr/textAppearanceMedium" />
    <CheckBox
        Android:id="@+id/itemDone"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        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_width="match_parent"
    Android:layout_height="match_parent"
    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>

Log

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)
128
user2734182

Ändern Sie in Ihrer public View getView-Methode return null; in return convertView;.

363
Pavya

#use return convertView; 

Code:

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;
}
13
Pong Petrung

in Ihrem baseadapter class Konstruktor versuchen Sie LayoutInflater zu initialisieren, normalerweise habe ich diesen Weg bevorzugt,

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

am oberen Rand der Klasse create LayoutInflater variable hoffen Sie, dass Ihnen dies helfen wird

2
Ashana.Jackol

es tritt manchmal auf, wenn wir einen benutzerdefinierten Adapter in einer Fragmentaktivität verwenden. und wir geben ein Nullobjekt zurück, d. h. eine Nullansicht, sodass die Aktivität durcheinander geraten wird, welche Ansicht geladen werden soll. Deshalb tritt diese Ausnahme auf

zeigt die Position, an der die Ansicht geändert werden soll

0
Aakib Jabbar