Android - 列表视图 - 启动新活动

时间:2013-01-19 13:51:52

标签: android android-listview

我正在尝试构建一个活动,其中列表视图中的每个列表项都会打开一个新活动。但每次我运行应用程序时都会被迫关闭。请帮助guyzz。我正在尝试很长一段时间!这是布局文件:

<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:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="C++ PROGRAMS" 
    android:textStyle="bold"
    android:textSize="25sp" />
<ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >

这是java类:

public class Second_listview extends ListActivity{
  static final String[] type = new String[]{

    "Array", "Strings" 

 };

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

    setContentView(R.layout.second_listview);

    // setting up list view

    setListAdapter (new ArrayAdapter<String>(this, android.R.id.list, type));
    ListView list = getListView();
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            //linking each list item to start a new activity

            switch(arg2)

            {
              case 1 : Intent myIntent1 = new Intent(view.getContext(), Array_list.class);
                       startActivityForResult(myIntent1, 0);
                       break;
              case 2 : Intent myIntent2 = new Intent(view.getContext(), String_list.class);
                       startActivityForResult(myIntent2, 0);
                       break;

            }

        }



    });
}

}

这是logcat:

01-19 13:26:03.123: E/AndroidRuntime(1323): FATAL EXCEPTION: Thread-132
01-19 13:26:03.123: E/AndroidRuntime(1323): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.c_progams.CLEARSCREEN }
01-19 13:26:03.123: E/AndroidRuntime(1323):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
01-19 13:26:03.123: E/AndroidRuntime(1323):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-19 13:26:03.123: E/AndroidRuntime(1323):     at android.app.Activity.startActivityForResult(Activity.java:3370)
01-19 13:26:03.123: E/AndroidRuntime(1323):     at android.app.Activity.startActivityForResult(Activity.java:3331)
01-19 13:26:03.123: E/AndroidRuntime(1323):     at android.app.Activity.startActivity(Activity.java:3566)
01-19 13:26:03.123: E/AndroidRuntime(1323):     at android.app.Activity.startActivity(Activity.java:3534)
01-19 13:26:03.123: E/AndroidRuntime(1323):     at com.example.c_progams.First_screen$1.run(First_screen.java:27)

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.c_progams"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.c_progams.First_screen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

     <activity
        android:name="com.example.c_progams.Second_listview"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.SECOND_LISTVIEW" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

 <activity
        android:name="com.example.c_progams.Array_list"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.ARRAY_LIST" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>


   <activity
        android:name="com.example.c_progams.String_list"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.STRING_LIST" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>



</application>


</manifest>

3 个答案:

答案 0 :(得分:0)

这是正确的代码......

<强> Java Code :

public class Sample extends ListActivity {
static final String[] type = new String[] { "Array", "Strings" };

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

    ListView list = getListView();
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, type));
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
            switch (arg2) {
            case 1:
                Intent myIntent1 = new Intent(view.getContext(), Array_list.class);
                startActivityForResult(myIntent1, 0);
                break;
            case 2:
                Intent myIntent2 = new Intent(view.getContext(), String_list.class);
                startActivityForResult(myIntent2, 0);
                break;

            }

        }

    });
}

}

<Application> TAG

中的AndroidManifest.xml中添加这两行
<application android:theme="@style/AppTheme" android:label="@string/app_name" android:icon="@drawable/ic_launcher"> 

            

答案 1 :(得分:0)

编辑:忽略我,这是在您使用清单编辑之前完成的。

您似乎尚未定义AndroidManifest.xml中的活动。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.package.name">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity1" android:label="@string/app_name"></activity>
        <activity android:name=".Activity2"></activity>
    </application>
    <uses-sdk android:minSdkVersion="4" />
</manifest>

答案 2 :(得分:0)

包裹名称不遵循驼峰套管或下划线或连字符。更改您的包裹名称,如

package="com.example.cprogams"

参考:

Naming Conventions

Package Name Validation

由于