Android片段在启动时崩溃

时间:2013-07-04 13:52:22

标签: java android android-fragments actionbarsherlock

当我尝试将动作条sherlock集成到我现有的项目中时,它会在启动时崩溃。 但是,在编译之前,操作栏和我的项目都正常工作。

这是我的片段代码:

public class SalesOrder extends SherlockFragment{

    ListView myInfoList;
    String[] items = {"Item 1", "Item 2"};
    ArrayAdapter<CharSequence> adapter;

    ArrayList<MessageDetails> details;
    AdapterView.AdapterContextMenuInfo info;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        refreshListView();

        return inflater.inflate(R.layout.sales_order, container, false);
    }

    private void refreshListView()
    {
        myInfoList = (ListView)getActivity().findViewById(R.id.MessageList);
        adapter = new ArrayAdapter<CharSequence>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, items);
        myInfoList.setAdapter(adapter);

        details = new ArrayList<MessageDetails>();
        MessageDetails Detail;

        //CHANGE THIS PART TO RETRIEVE DATABASE ITEM DYNAMICALLY
        List<String> itemName = new ArrayList<String>();
        itemName.add("Shirt Design 1");
        itemName.add("Shirt Design 2");
        itemName.add("Shirt Design 3");

        List<Float> itemPrice = new ArrayList<Float>();
        itemPrice.add(14f);
        itemPrice.add(15f);
        itemPrice.add(16f);

        List<String> description = new ArrayList<String>();
        description.add("This is a fine shirt 1");
        description.add("This is a fine shirt 2");
        description.add("This is a fine shirt 3");

        Detail = new MessageDetails();

        for (int i =0; i < itemName.size(); i++)
        {
            Detail = new MessageDetails();
            Detail.setSub(itemName.get(i));
            Detail.setSubValue(itemPrice.get(i));
            Detail.setDescription(description.get(i));
            details.add(Detail);
        }   

        myInfoList.setAdapter(new CustomAdapter(details , getActivity().getApplicationContext()));

        myInfoList.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                   //System.out.println("Name: "+details.get(position).getSub());
                   String s = details.get(position).getDescription();
               }
       });  
    }

    public class CustomAdapter extends BaseAdapter {

        private ArrayList<MessageDetails> _data;
        Context _c;

        CustomAdapter (ArrayList<MessageDetails> data, Context c){
            _data = data;
            _c = c;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return _data.size();
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return _data.get(position);
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
             View v = convertView;
             if (v == null) 
             {
                LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.mysaleslistview, null);
             }
               TextView subView = (TextView)v.findViewById(R.id.itemName);
               TextView subValueView = (TextView)v.findViewById(R.id.itemPrice);

               MessageDetails msg = _data.get(position);
               subView.setText(msg.sub);    
               subValueView.setText(msg.subValue.toString());             

            return v; 
    }
    }

    public class MessageDetails {
        String sub, description;
        Float subValue;

        public String getSub() {
            return sub;
        }

        public void setSub(String sub) {
            this.sub = sub;
        }

        public Float getSubValue() {
            return subValue;
        }

        public void setSubValue(Float subValue) {
            this.subValue = subValue;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }
}

我的logcat:

07-04 21:46:56.264: E/Trace(6016): error opening trace file: No such file or directory (2)
07-04 21:46:56.318: I/ActionBarSherlock(6016): Registering ActionBarSherlockCompat with qualifier @com.actionbarsherlock.ActionBarSherlock$Implementation(api=7, dpi=-1)
07-04 21:46:56.318: I/ActionBarSherlock(6016): Registering ActionBarSherlockNative with qualifier @com.actionbarsherlock.ActionBarSherlock$Implementation(api=14, dpi=-1)
07-04 21:46:56.318: I/ActionBarSherlock(6016): Using implementation: ActionBarSherlockNative
07-04 21:46:56.318: D/ActionBarSherlock(6016): [<ctor>] activity: com.example.isellcrazyitem.ISellCrazyItem@41323460, flags: 1
07-04 21:46:56.318: D/ActionBarSherlock(6016): [setContentView] view: android.support.v4.view.ViewPager{41326568 VFED.... ......I. 0,0-0,0 #7f040032 app:id/pager}
07-04 21:46:56.318: D/ActionBarSherlock(6016): [setContentView] view: android.support.v4.view.ViewPager{41326568 VFED.... ......I. 0,0-0,0 #7f040032 app:id/pager}, params: android.view.ViewGroup$LayoutParams@4137b8f0
07-04 21:46:56.389: D/ActionBarSherlock(6016): [getActionBar]
07-04 21:46:56.389: D/ActionBarSherlock(6016): [getActionBar]
07-04 21:46:56.404: V/21st Polling:(6016): clicked
07-04 21:46:56.443: D/AndroidRuntime(6016): Shutting down VM
07-04 21:46:56.443: W/dalvikvm(6016): threadid=1: thread exiting with uncaught exception (group=0x41080930)
07-04 21:46:56.443: E/AndroidRuntime(6016): FATAL EXCEPTION: main
07-04 21:46:56.443: E/AndroidRuntime(6016): java.lang.NullPointerException
07-04 21:46:56.443: E/AndroidRuntime(6016):     at com.example.isellcrazyitem.SalesOrder.refreshListView(SalesOrder.java:38)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at com.example.isellcrazyitem.SalesOrder.onCreateView(SalesOrder.java:29)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.support.v4.view.ViewPager.populate(ViewPager.java:804)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.View.measure(View.java:15523)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.View.measure(View.java:15523)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.View.measure(View.java:15523)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2434)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.View.measure(View.java:15523)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.Choreographer.doFrame(Choreographer.java:532)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.os.Handler.handleCallback(Handler.java:725)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.os.Looper.loop(Looper.java:137)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at android.app.ActivityThread.main(ActivityThread.java:5233)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at java.lang.reflect.Method.invoke(Method.java:511)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
07-04 21:46:56.443: E/AndroidRuntime(6016):     at dalvik.system.NativeStart.main(Native Method)
07-04 21:47:07.107: I/Process(6016): Sending signal. PID: 6016 SIG: 9

1 个答案:

答案 0 :(得分:1)

将您的refreshListView()代码移动到onActivityCreated,它可以正常工作。