单击列表视图后,Android应用程序自动关闭

时间:2015-01-30 21:12:54

标签: android android-layout listview android-intent android-listview

我有滑块应用程序,在使用左键滑动时会有一个列表视图,在点击列表视图中应该打开第二个activity.i使用Intent,但我的Android应用程序在点击列表视图后会自动关闭。

MainActivity.java

package com.example.slide_menu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.navdrawer.SimpleSideDrawer;

public class MainActivity extends Activity {
    SimpleSideDrawer slide_me;
    Button left_button, right_button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        slide_me = new SimpleSideDrawer(this);
        slide_me.setLeftBehindContentView(R.layout.left_menu);
        slide_me.setRightBehindContentView(R.layout.right_menu);

        left_button = (Button) findViewById(R.id.left_buton);
        right_button = (Button) findViewById(R.id.right_buton);
        left_button.setOnClickListener(new View.OnClickListener() {
            private ListView l1;
            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                slide_me.toggleLeftDrawer();
                 l1=(ListView)findViewById(R.id.listView1);
                   l1.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.dataelements)));

                   l1.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                View view, int position, long id) {

                // TODO Auto-generated method stub


                            switch(position){


                            case 0:  Intent newActivity0 = new Intent(MainActivity.this,Lone.class);     
                            startActivity(newActivity0);
                            break;
//                 case 1:  Intent newActivity1 = new Intent(MainActivity.this,Maruti.class);     
//                          startActivity(newActivity1);
//                          break;
//                 case 2:  Intent newActivity = new Intent(this, olympiakos.class);     
//                          startActivity(newActivity);
//                          break;
//                 case 3:  Intent newActivity = new Intent(this, karaiskaki.class);     
//                          startActivity(newActivity);
//                          break;
//                 case 4:  Intent newActivity = new Intent(this, reservetickets.class);     
//                          startActivity(newActivity);
//                          break;



                            }

                        }
                        });
            }           
            });


        right_button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                slide_me.toggleRightDrawer();
            }
        });
    }
}

left_menu

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="260dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Left Menu"
            android:textColor="#000000" />
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
</LinearLayout>

String.xml

<resources>

    <string name="app_name">slide_menu</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">Slide Demo</string>


     <string-array name="dataelements">
        <item>one</item>
        <item>two</item>
        <item>three</item>
        <item>four</item>
        <item>five</item>

    </string-array>


</resources>

Lone.java(mysecondactivityfile)

package com.example.slide_menu;

import android.app.Activity;
import android.os.Bundle;

public class Lone extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lo);
    }
}

Lo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="lone scond"
    android:textAppearance="?android:attr/textAppearanceLarge" />

0 个答案:

没有答案