0

我是 android 的新手,我正在创建一个应用程序。
应用程序布局配置有Swipe Views。其中一个片段具有已ListView完成操作的历史记录。当用户点击一个按钮时,应用程序会挑选一些数据并将其放在一个ListView.膨胀的布局是一个自定义布局。但是当我尝试单击按钮时,应用程序停止工作。看到:

适配器

public class ChronoListAdapter extends ArrayAdapter<RegExpItem> {

    private ArrayList<RegExpItem> regExpItems = null;
    private Context context;

    public ChronoListAdapter(Context context, int resource, ArrayList<RegExpItem> objects) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        //Inflate Row and retrieve TextViews and ImageViews
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View listItem = inflater.inflate(R.layout.list_item, parent, false);
        TextView regExpItem = (TextView) listItem.findViewById(R.id.RegExpList);
        TextView stringItem = (TextView) listItem.findViewById(R.id.StringList);
        ImageView matchItem = (ImageView) listItem.findViewById(R.id.MatchList);

        //Insert Strings
        regExpItem.setText(regExpItems.get(position).regExp);
        stringItem.setText(regExpItems.get(position).strings);

        //Change image for true or false
        if (regExpItems.get(position).match) {
            matchItem.setImageResource(R.drawable.ic_checkmark_holo_light);
        } else {
            matchItem.setImageResource(R.drawable.ic_clear_search_api_holo_light);
        }
        return listItem;
    }
}

ChronoFragment 上的实现 (onCreateView())

点击

RegExpItem rei = new RegExpItem(regExp, input, true);
regExpItems.add(rei);
adapter.notifyDataSetChanged();

Eclipse 没有给出任何问题,我也没有构建问题。我该如何解决?

PS:ArrayList当我将它分配给我的适配器时,它是空的。

编辑:经过一些尝试,我认为当我添加项目时应用程序崩溃ArrayList

堆栈跟踪

Thread [<1> main] (Suspended (exception NullPointerException))  
<VM does not provide monitor information>   
Choreographer.doCallbacks(int, long) line: 572  
Choreographer.doFrame(long, int) line: 532  
Choreographer$FrameDisplayEventReceiver.run() line: 735 
Handler.handleCallback(Message) line: 730   
Choreographer$FrameHandler(Handler).dispatchMessage(Message) line: 92   
Looper.loop() line: 137 
ActivityThread.main(String[]) line: 5289    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 525  
ZygoteInit$MethodAndArgsCaller.run() line: 739  
ZygoteInit.main(String[]) line: 555 
NativeStart.main(String[]) line: not available [native method]  

日志猫

11-08 11:06:38.360: E/AndroidRuntime(10188): FATAL EXCEPTION: main
11-08 11:06:38.360: E/AndroidRuntime(10188): java.lang.NullPointerException
11-08 11:06:38.360: E/AndroidRuntime(10188): at com.slinky.regexptool.ChronoListAdapter.getView(ChronoListAdapter.java:30)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.widget.AbsListView.obtainView(AbsListView.java:2177)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.widget.ListView.makeAndAddView(ListView.java:1840)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.widget.ListView.fillDown(ListView.java:675)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.widget.ListView.fillFromTop(ListView.java:736)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.widget.ListView.layoutChildren(ListView.java:1655)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.widget.AbsListView.onLayout(AbsListView.java:2012)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.View.layout(View.java:14296)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.View.layout(View.java:14296)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.View.layout(View.java:14296)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.View.layout(View.java:14296)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.View.layout(View.java:14296)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.View.layout(View.java:14296)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.Choreographer.doFrame(Choreographer.java:532)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.os.Handler.handleCallback(Handler.java:730)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.os.Looper.loop(Looper.java:137)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at android.app.ActivityThread.main(ActivityThread.java:5289)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at java.lang.reflect.Method.invokeNative(Native Method)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at java.lang.reflect.Method.invoke(Method.java:525)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
11-08 11:06:38.360: E/AndroidRuntime(10188):    at dalvik.system.NativeStart.main(NativeMethod)
4

1 回答 1

0

我认为您需要将其添加到 ChronoListAdapter 的构造函数中:

this.regExpItems = objects;

如果这是问题所在,那么您的应用程序会在 NullPointerException 上崩溃。您可以使用 LogCat 在 Eclipse 中查看 Android 的堆栈跟踪(窗口 -> 显示视图 -> 其他... -> LogCat。

于 2013-11-07T19:47:12.097 回答