ListView.setAdapter NullPointerException

时间:2015-04-27 17:22:33

标签: java android listview

当我抓住NPE时,我正在为课程做作业。无法解决此问题几天,无法修复。谷歌没有帮助。 更新代码 这是我的代码:

MainActivity

// try to use mysqli_* instead of mysql_* functions, mysqli_query() expects parameter 1 to be a database connection resource
$res = mysqli_query($connection, "SELECT name, nickname FROM Horseinfo WHERE name = '$somevariable'");
if ($res && mysqli_num_rows($res)>0) {
  $row = mysqli_fetch_row($res); 
  $horseName= empty($row['nickname']) ? $row['name'] : $row['nickname'];
  ?>
  <div style='font-size:18px; padding-bottom:3px; margin-top:0px;'>records for <? echo horseName;?></div>
<?php } ?>

自拍:

List<Selfie> selfies = new ArrayList<Selfie>();
private CustomAdapter ADAPTER = new CustomAdapter(this, selfies);

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

@Override
protected void onResume() {
    super.onResume();
    File youDir = new File("/sdcard/DailySelfie/");

    outer : for (File f : youDir.listFiles()) {
        //try to find selfie in files
        if (f.isFile()) {
            for (Selfie selfie : selfies) {
                if (selfie.getSelfieName() == f.getName()) {
                    continue outer;
                }
                else { continue;}
            }
            //code below works only if file doesn't exist in selfies.
            Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
            try {
                 selfies.add(new Selfie(f.getName(), myBitmap));
            }  catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

CustomAdapter:

public class Selfie {
private Bitmap selfieBitmap;
private String selfieName;
public Selfie(String selfieName, Bitmap selfieBitmap) {
    this.selfieName = selfieName;
    this.selfieBitmap = selfieBitmap;
}
public String getSelfieName() {
    return selfieName;
}
public Bitmap getSelfieBitmap() {
    return selfieBitmap;
}
}

ADB:

private Context context;
private List<Selfie> selfies = new ArrayList<Selfie>();

public CustomAdapter(Context context, List<Selfie> selfies) {
    this.context = context;
    this.selfies = selfies;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
    Selfie selfie = selfies.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = new View(context);
        //get layout from row.xml
        rowView = inflater.inflate(R.layout.row, null);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
        imageView.setImageBitmap(selfie.getSelfieBitmap());
        TextView textView = (TextView) rowView.findViewById(R.id.text);
        textView.setText(selfie.getSelfieName());
    }
    return convertView;
}
}

似乎我 04-27 13:17:58.792: E/AndroidRuntime(1979): FATAL EXCEPTION: main 04-27 13:17:58.792: E/AndroidRuntime(1979): java.lang.NullPointerException 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.AbsListView.obtainView(AbsListView.java:2179) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.ListView.makeAndAddView(ListView.java:1840) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.ListView.fillDown(ListView.java:675) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.ListView.fillFromTop(ListView.java:736) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.ListView.layoutChildren(ListView.java:1655) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.AbsListView.onLayout(AbsListView.java:2012) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.onLayout(LinearLayout.java:1436) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.Choreographer.doFrame(Choreographer.java:532) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.os.Handler.handleCallback(Handler.java:730) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.os.Handler.dispatchMessage(Handler.java:92) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.os.Looper.loop(Looper.java:137) 04-27 13:17:58.792: E/AndroidRuntime(1979): at android.app.ActivityThread.main(ActivityThread.java:5103) 04-27 13:17:58.792: E/AndroidRuntime(1979): at java.lang.reflect.Method.invokeNative(Native Method) 04-27 13:17:58.792: E/AndroidRuntime(1979): at java.lang.reflect.Method.invoke(Method.java:525) 04-27 13:17:58.792: E/AndroidRuntime(1979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 04-27 13:17:58.792: E/AndroidRuntime(1979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 04-27 13:17:58.792: E/AndroidRuntime(1979): at dalvik.system.NativeStart.main(Native Method) 有一个NPE。在StackOverflow上发现了同样的问题,但没有任何解决方案对我有帮助。 更新:重写了我的代码,现在它已经

lv.setAdapter(ADAPTER);

还有同样的问题 - NPE。我试图移动&#34; lv = ..; ADAPTER = ...&#34;到onResume,试图把它开始onResume - 它没有帮助:(

7 个答案:

答案 0 :(得分:1)

添加

private CustomAdapter ADAPTER = new CustomAdapter(this, selfies);

进入onCreate方法,因为this上下文用于类方法.i.e。重写为

 private CustomAdapter ADAPTER;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ADAPTER = new CustomAdapter(this, selfies);
    }

答案 1 :(得分:0)

检查是否返回null以外的任何内容:

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

可能无法找到列表。

答案 2 :(得分:0)

由于您在ListView lv = (ListView) findViewById(R.id.listView);中呼叫onResume,因此尚未调用setContentView。始终在onResume之前调用onCreate,因此您的ListView目前不在上下文中。因此NullPointerException。

将您的活动代码更改为此

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView lv = (ListView) findViewById(R.id.listView);

}

@Override
protected void onResume() {
    super.onResume();
}

答案 3 :(得分:0)

当你初步设定新的自定义时,问题在于你已经设定了这个问题....你必须初始化它onCreate()! (我的意思是MainActivity类)

答案 4 :(得分:0)

我相信NPE与你的ListView有关。尝试构建您的应用程序,以便声明类变量并在onCreate()中初始化它们。这样,您稍后会引用它们(就像在onResume()中那样)。

List<Selfie> selfies;
private CustomAdapter ADAPTER;
ListView lv;

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

    lv = (ListView) findViewById(R.id.listView);
    selfies = new ArrayList<Selfie>();
    ADAPTER = new CustomAdapter(this, selfies);

    lv.setAdapter(ADAPTER);
}

@Override
protected void onResume() {
    super.onResume();
    /*
    *I don't quite see the purpose in this below section, 
    *as you can account for a null ArrayList in your adapter class
    */
    if (ADAPTER.getCount() >= 0) {
    lv.setAdapter(ADAPTER);
    }

}

另外,为了更好地解释我上面提到的评论,您可以通过将空数据视为getCount()大小列表来解释适配器0中的空数据:

getCount() {
    if (selfies == null || selfies.isEmpty()) {
        return 0;
    }
    return selfies.size;
}

编辑:如果问题仅限于您的适配器,那么它可能是来自getView()的NPE。您可以尝试以下代码:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView;
            Selfie selfie = selfies.get(position);

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) parent.getContext().
                        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                rowView = inflater.inflate(R.layout.row, parent, false);
            } else {
                rowView = convertView;
            }
            ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
            imageView.setImageBitmap(selfie.getSelfieBitmap());
            TextView textView = (TextView) rowView.findViewById(R.id.text);
            textView.setText(selfie.getSelfieName());

            return rowView;
        }

答案 5 :(得分:0)

你自拍的arraylist是空的,因为我什么都没看到? 你的班级Selfie只有吸气剂,但没有固定器,所以你如何设置自拍的arraylist自拍呢?

答案 6 :(得分:0)

答案是,在我的getView()代码中,我返回了convertView但却膨胀了rowView。所以convertView保持为null。 PPartisan向我展示了我的错误并给出了工作代码(如下)。为了提高评级我必须有15个声誉,而我没有。请问某人,他的评价和正确答案?

    public View getView(int position, View convertView, ViewGroup parent) {
    View rowView;
    Selfie selfie = selfies.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext().
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.row, parent, false);
    } else {
        rowView = convertView;
    }
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
    imageView.setImageBitmap(selfie.getSelfieBitmap());
    TextView textView = (TextView) rowView.findViewById(R.id.text);
    textView.setText(selfie.getSelfieName());

    return rowView;
}