使用listview时出现空适配器错误

时间:2016-02-19 19:20:10

标签: java android listview nullpointerexception android-arrayadapter

在我的应用程序中,我尝试将String Arraylist从一个Activity转移到另一个Activity,并在listview中显示它们。但是在我设置适配器之后,我得到了一个空指针异常,并且应用程序崩溃了。

错误在Line31上,我已标记它。

MainActivity

    final List<DataHolder> dataList = new ArrayList<DataHolder>();
    DataHolder cheeseburger = new DataHolder("Cheese Burger","5.00",false);
    dataList.add(cheeseburger);
    DataHolder turkeyburger = new DataHolder("Turkey Burger","6.50",false);
    dataList.add(turkeyburger);
    DataHolder fishburger = new DataHolder("Fish Burger","7.00",false);
    dataList.add(fishburger);
    DataHolder applepie = new DataHolder("Apple Pie","5.00",false);
    dataList.add(applepie);
    DataHolder orangejuice = new DataHolder("Orange Juice","4.50",false);

    Button order = (Button) findViewById(R.id.play_order);

    order.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          ArrayList<String> Select = new ArrayList<String>();
            double total = 0;
            for (int i = 0; i < dataList.size(); i++) {
                if (dataList.get(i).checked){
                    total = total+Double.valueOf(dataList.get(i).Price);
                    Select.add(dataList.get(i).Dish);
                }
                dataList.get(i).checked = false;
            }
            Log.d("checked", Select.get(1));
            Intent intent = new Intent(MainActivity.this,OrderActivity.class);
            intent.putExtra("totalprice", total);
            intent.putStringArrayListExtra("SelectDish",Select);
            startActivityForResult(intent, 1);
        }
    });

OrderActivity

public class OrderActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayList<String> show = new ArrayList<String>();
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        Intent intent = getIntent();
        final Double Total =intent.getDoubleExtra("totalprice", 0);
        listView = (ListView) findViewById(R.id.list_view);
        show = intent.getStringArrayListExtra("SelectDish");
        adapter = new ArrayAdapter<String>(OrderActivity.this,android.R.layout.simple_list_item_1,show);
LINE31  listView.setAdapter(adapter);        
        TextView price =(TextView) findViewById(R.id.price);
        price.setText("Total Price: $"+String.valueOf(Total));
 java.lang.RuntimeException: Unable to start activity ComponentInfo{angel.assignment4/angel.assignment4.OrderActivity}: java.lang.NullPointerException
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
   at android.app.ActivityThread.access$600(ActivityThread.java:162)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:158)
   at android.app.ActivityThread.main(ActivityThread.java:5751)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
   at angel.assignment4.OrderActivity.onCreate(OrderActivity.java:31)
   at android.app.Activity.performCreate(Activity.java:5165)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520) 
   at android.app.ActivityThread.access$600(ActivityThread.java:162) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366) 
   at android.os.Handler.dispatchMessage(Handler.java:99) 
   at android.os.Looper.loop(Looper.java:158) 
   at android.app.ActivityThread.main(ActivityThread.java:5751) 
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:511) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850) 
   at dalvik.system.NativeStart.main(Native Method) 

enter image description here

0 个答案:

没有答案