adapter.getView中的NullPointer异常发生原因不明

时间:2017-05-07 08:52:16

标签: java android nullpointerexception

  

adapter.getView

中的NullPointer异常

我不知道原因。

//////////this is the code

package com.example.said.notetoself;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private NoteAdapter mNoteAdapter;
    private boolean mSound;
    private int mAnimOption;
    private SharedPreferences mPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mNoteAdapter = new NoteAdapter();

        ListView listNote = (ListView) findViewById(R.id.listView);

        listNote.setAdapter(mNoteAdapter);

        listNote.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        Note tempNote = (Note) mNoteAdapter.getItem(position);

                        DialogShowNote dialog = new DialogShowNote();

                        dialog.sendNoteSelected(tempNote);
                        dialog.show(getFragmentManager(), "");
                    }
                }
        );

    }
    @Override
    protected void onResume(){
        super.onResume();
        mPrefs = getSharedPreferences("Note to self", MODE_PRIVATE);
        mSound = mPrefs.getBoolean("sound", true);
        mAnimOption = mPrefs.getInt("anim option", SettingsActivity.fast);
    }

    public void createNewNote(Note n){
        mNoteAdapter.addNote(n);
    }

    @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 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_add) {

            DialogNewNote dialog = new DialogNewNote();
            dialog.show(getFragmentManager(), "");

            return true;
        }

        if(id == R.id.action_settings){

            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public class NoteAdapter extends BaseAdapter{

        List<Note> noteList = new ArrayList<>();

        @Override
        public int getCount() {
            return noteList.size();
        }

        @Override
        public Object getItem(int position) {
            return noteList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {

            if (view == null){

                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                view = inflater.inflate(R.layout.listitem, parent, false);

            }

            TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
            TextView txtDescription = (TextView) findViewById(R.id.txtDescription);
            ImageView ivImportant  =(ImageView) findViewById(R.id.imageViewImportant);
            ImageView ivTodo = (ImageView) findViewById(R.id.imageViewTodo);
            ImageView ivIdea = (ImageView) findViewById(R.id.imageViewIdea);

            Note tempNote = noteList.get(position);

            if (!tempNote.isImportant()) ivImportant.setVisibility(View.INVISIBLE);
            if (!tempNote.isTodo()) ivTodo.setVisibility(View.INVISIBLE);
            if (!tempNote.isIdea()) ivIdea.setVisibility(View.INVISIBLE);

            txtTitle.setText(tempNote.getTitle());
            txtDescription.setText(tempNote.getDescription());

            return view;
        }

        public void addNote(Note n){

            noteList.add(n);
            notifyDataSetChanged();
        }
    }

}

/////////这是出现的问题

E/AndroidRuntime: FATAL EXCEPTION: main
          Process: com.example.said.notetoself, PID: 1791
          java.lang.NullPointerException
              at com.example.said.notetoself.MainActivity$NoteAdapter.getView(MainActivity.java:142)
              at android.widget.AbsListView.obtainView(AbsListView.java:2255)
              at android.widget.ListView.makeAndAddView(ListView.java:1790)
              at android.widget.ListView.fillDown(ListView.java:691)
              at android.widget.ListView.fillFromTop(ListView.java:752)
              at android.widget.ListView.layoutChildren(ListView.java:1616)
              at android.widget.AbsListView.onLayout(AbsListView.java:2087)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
              at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
              at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1364)
              at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:846)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
              at android.view.View.layout(View.java:14817)
              at android.view.ViewGroup.layout(ViewGroup.java:4631)
              at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983)
              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)
              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
              at android.view.Choreographer.doCallbacks(Choreographer.java:574)
              at android.view.Choreographer.doFrame(Choreographer.java:544)
              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
              at android.os.Handler.handleCallback(Handler.java:733)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5001)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
              at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

来自视图引用的适配器findViewById的side getView方法,如

 TextView txtTitle = (TextView) view.findViewById(R.id.txtTitle);
        TextView txtDescription = (TextView) view.findViewById(R.id.txtDescription);
        ImageView ivImportant  =(ImageView) view.findViewById(R.id.imageViewImportant);
        ImageView ivTodo = (ImageView) view.findViewById(R.id.imageViewTodo);
        ImageView ivIdea = (ImageView) view.findViewById(R.id.imageViewIdea);