0

我正在使用 JSON 将数据提取到列表视图中,但现在我需要制作我的列表视图以允许用户选择多个项目并在底部显示添加到订单按钮,一旦用户完成了复选框选择并且现在想要制作该订单只需要单击添加到订单按钮,一旦用户单击按钮然后选择 ListView 项目,我将使用 Intent 即 ResultActivity 发送到下一个活动,但是每当我运行我的应用程序时,不幸的是应用程序已停止并且 Logcat 说:

   10-19 12:56:56.812: D/dalvikvm(709): GC_FOR_ALLOC freed 53K, 3% free 8069K/8259K, paused 43ms, total 45ms
10-19 12:56:56.812: I/dalvikvm-heap(709): Grow heap (frag case) to 8.514MB for 614416-byte allocation
10-19 12:56:56.882: D/dalvikvm(709): GC_CONCURRENT freed 1K, 3% free 8668K/8903K, paused 23ms+4ms, total 70ms
10-19 12:56:56.932: D/dalvikvm(709): GC_FOR_ALLOC freed 0K, 3% free 8668K/8903K, paused 30ms, total 30ms
10-19 12:56:56.964: I/dalvikvm-heap(709): Grow heap (frag case) to 9.832MB for 1382416-byte allocation
10-19 12:56:57.043: D/dalvikvm(709): GC_CONCURRENT freed 0K, 3% free 10018K/10311K, paused 36ms+5ms, total 83ms
10-19 12:56:57.293: D/gralloc_goldfish(709): Emulator without GPU emulation detected.
10-19 12:57:00.862: D/AndroidRuntime(709): Shutting down VM
10-19 12:57:00.862: W/dalvikvm(709): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-19 12:57:00.924: E/AndroidRuntime(709): FATAL EXCEPTION: main
10-19 12:57:00.924: E/AndroidRuntime(709): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.erachnida.restaurant.versionoct/com.erachnida.restaurant.versionoct.TabSample}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.erachnida.restaurant.versionoct/com.erachnida.restaurant.versionoct.MainActivity}: java.lang.NullPointerException
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.os.Looper.loop(Looper.java:137)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-19 12:57:00.924: E/AndroidRuntime(709):  at java.lang.reflect.Method.invokeNative(Native Method)
10-19 12:57:00.924: E/AndroidRuntime(709):  at java.lang.reflect.Method.invoke(Method.java:511)
10-19 12:57:00.924: E/AndroidRuntime(709):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-19 12:57:00.924: E/AndroidRuntime(709):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-19 12:57:00.924: E/AndroidRuntime(709):  at dalvik.system.NativeStart.main(Native Method)
10-19 12:57:00.924: E/AndroidRuntime(709): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.erachnida.restaurant.versionoct/com.erachnida.restaurant.versionoct.MainActivity}: java.lang.NullPointerException
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1900)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:694)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.widget.TabHost.setCurrentTab(TabHost.java:358)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.widget.TabHost.addTab(TabHost.java:236)
10-19 12:57:00.924: E/AndroidRuntime(709):  at com.erachnida.restaurant.versionoct.TabSample.addTab(TabSample.java:48)
10-19 12:57:00.924: E/AndroidRuntime(709):  at com.erachnida.restaurant.versionoct.TabSample.setTabs(TabSample.java:27)
10-19 12:57:00.924: E/AndroidRuntime(709):  at com.erachnida.restaurant.versionoct.TabSample.onCreate(TabSample.java:23)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.Activity.performCreate(Activity.java:5008)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-19 12:57:00.924: E/AndroidRuntime(709):  ... 11 more
10-19 12:57:00.924: E/AndroidRuntime(709): Caused by: java.lang.NullPointerException
10-19 12:57:00.924: E/AndroidRuntime(709):  at com.erachnida.restaurant.versionoct.MainActivity.onCreate(MainActivity.java:39)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.Activity.performCreate(Activity.java:5008)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-19 12:57:00.924: E/AndroidRuntime(709):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-19 12:57:00.924: E/AndroidRuntime(709):  ... 23 more

MainActivity.java:-

public class MainActivity extends Activity implements OnItemClickListener, OnClickListener {
    Button button;
ListView lview3;
ListViewCustomAdapter adapter;
private ArrayList<Object> itemList;
private ItemBean bean;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    prepareArrayLits();
    lview3 = (ListView) findViewById(R.id.listView1);
    adapter = new ListViewCustomAdapter(this, itemList);
    lview3.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lview3.setAdapter(adapter);

    lview3.setOnItemClickListener(this);
    button.setOnClickListener((OnClickListener) this);
}

private void findViewsById() {
    lview3 = (ListView) findViewById(R.id.listView1);
    button = (Button) findViewById(R.id.submit);
}

public void onClick(View v) {
    SparseBooleanArray checked = lview3.getCheckedItemPositions();
    ArrayList<String> selectedItems = new ArrayList<String>();
    for (int i = 0; i < checked.size(); i++) {
        // Item position in adapter
        int position = checked.keyAt(i);
        // Add sport if it is checked i.e.) == TRUE!
        if (checked.valueAt(i))
            selectedItems.add((String) adapter.getItem(position));
    }
    String[] outputStrArr = new String[selectedItems.size()];

    for (int i = 0; i < selectedItems.size(); i++) {
        outputStrArr[i] = selectedItems.get(i);
    }

    Intent intent = new Intent(getApplicationContext(),
            ResultActivity.class);

    // Create a bundle object
    Bundle b = new Bundle();
    b.putStringArray("selectedItems", outputStrArr);

    // Add the bundle to the intent.
    intent.putExtras(b);

    // start the ResultActivity
    startActivity(intent);
}

ResultActivity.java:-

   public class ResultActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);

        Bundle b = getIntent().getExtras();
        String[] resultArr = b.getStringArray("selectedItems");
        ListView lv = (ListView) findViewById(R.id.outputList);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, resultArr);
        lv.setAdapter(adapter);
    }
}

活动主要:-

<Button
    android:id="@+id/submit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Add to Order" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/header"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="20dp"
    android:cacheColorHint="#00000000"
    android:divider="#00000000"
    android:dividerHeight="20dp" 
    android:layout_above="@id/submit"
    >

</ListView>

选项卡示例:-

   public class TabSample extends TabActivity {
    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabmain);
        setTabs() ;
    }
    private void setTabs()
    {
        addTab("Menu", R.drawable.tab_home, MainActivity.class);
        addTab("Offers", R.drawable.tab_search, MainActivity.class);

        addTab("Location", R.drawable.tab_home, ArrowsActivity.class);
        addTab("Reservation", R.drawable.tab_search, ReservationActivity.class);
    }

    private void addTab(String labelId, int drawableId, Class<?> c)
    {
        TabHost tabHost = getTabHost();
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }
}
4

0 回答 0