我正在android中开发一个应用程序,我在其中创建了几个项目的列表视图。 我想在每个列表项上点击一个类click.Here是我的代码
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.ListView;
import android.widget.Toast;
import com.example.listview.abc;
public class listview extends Activity implements OnItemClickListener {
public ListView lv1;
public String[] names = { "Abc", "Xyz", "Pqr", "Jap", "Jay",
"bla bla" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1 = (ListView) findViewById(R.id.list);
lv1.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, names));
lv1.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
if(position==0){
Intent i = new Intent(this, abc.class);
startActivity(i);
//Toast.makeText(this, "You pressed the first item in the list",
// Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "You pressed all other items in the list",
Toast.LENGTH_SHORT).show();
}
// else if(position==1){
// start another activity here...
}
问题是,当我点击第一个项目时它会崩溃,但点击其他项目会显示该消息,但是当我点击下一个活动的项目时它会崩溃 我试图遵循stackoverflow的这个问题,但它没有奏效 { How to jump from ListView to next Activity } 这是log cat的一部分
06-09 12:29:52.111: DEBUG/AndroidRuntime(903): Shutting down VM
06-09 12:29:52.181: INFO/AndroidRuntime(903): NOTE: attach of thread 'Binder Thread #3' failed
06-09 12:29:52.181: INFO/ActivityManager(60): Start proc com.example.listview for activity com.example.listview/.listview: pid=910 uid=10039 gids={}
06-09 12:29:52.310: DEBUG/dalvikvm(903): Debugger has detached; object registry had 1 entries
06-09 12:29:54.670: INFO/ActivityManager(60): Displayed activity com.example.listview/.listview: 2616 ms (total 199904 ms)
06-09 12:29:55.249: DEBUG/dalvikvm(253): GC_EXPLICIT freed 176 objects / 12384 bytes in 3461ms
06-09 12:29:59.940: DEBUG/dalvikvm(125): GC_EXPLICIT freed 878 objects / 48664 bytes in 188ms
06-09 12:30:05.270: DEBUG/dalvikvm(262): GC_EXPLICIT freed 46 objects / 2240 bytes in 183ms
06-09 12:30:50.140: DEBUG/SntpClient(60): request time failed: java.net.SocketException: Address family not supported by protocol
06-09 12:32:52.731: DEBUG/dalvikvm(120): GC_FOR_MALLOC freed 9850 objects / 473648 bytes in 1222ms
06-09 12:35:50.171: DEBUG/SntpClient(60): request time failed: java.net.SocketException: Address family not supported by protocol
06-09 12:40:50.240: DEBUG/SntpClient(60): request time failed: java.net.SocketException: Address family not supported by protocol
06-09 12:45:50.311: DEBUG/SntpClient(60): request time failed: java.net.SocketException: Address family not supported by protocol
最明显的文件是
**
**
<?xml version="1.0" encoding="utf-8"?>
> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
> package="com.example.listview" android:versionCode="1"
> android:versionName="1.0">
> <application android:icon="@drawable/icon" android:label="@string/app_name">
> <activity android:name=".listview" android:label="@s tring/app_name">
>
> <intent-filter>
> <action android:name="android.intent.action.MAIN" />
> <category android:name="android.intent.category.LAUNCHER" />
> </intent-filter>
> </activity>
>
> <activity android:enabled="true" android:name="abc" />
> </application>
> <uses-sdk android:minSdkVersion="3" />
> <uses-permission android:name="android.permission.INTERNET" />
> </manifest>
**
**
请查看代码并帮我解决
提前致谢..
答案 0 :(得分:3)
public class ListItemIntent extends Activity implements OnItemClickListener {
ListView lv1;
String[] names = { "Abc", "Xyz", "Pqr", "Jap", "Jay", "Blabla" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1 = (ListView) findViewById(R.id.list);
lv1.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, names));
lv1.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent intent;
switch (arg2) {
case 0:
intent= new Intent(this,Abc.class);
startActivity(intent);
break;
case 1:
intent=new Intent(this,Xyz.class);
startActivity(intent);
break;
case 2:
intent=new Intent(this,Pqr.class);
startActivity(intent);
break;
case 3:
intent=new Intent(this,Jap.class);
startActivity(intent);
break;
case 4:
intent=new Intent(this,Jay.class);
startActivity(intent);
break;
case 5:
intent=new Intent(this,Blabla.class);
startActivity(intent);
break;
default:
break;
}
}
}
对于每个类,您必须在Androidmanifest.xml中添加Activity。并且表现得像..
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ListItemIntent"
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="Abc"></activity>
<activity android:name="Blabla"></activity>
<activity android:name="Jap"></activity>
<activity android:name="Jay"></activity>
<activity android:name="Pqr"></activity>
<activity android:name="Xyz"></activity>
</application>
答案 1 :(得分:2)
您是否在AndroidManifest.xml文件中正确注册了abc.class
作为活动?这会导致崩溃。
答案 2 :(得分:1)
请在AndroidManifest.xml中更改以下
<activity android:name=".abc" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
还添加以下行
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
然后尝试一下。
如果不成功,请检查以下链接。
答案 3 :(得分:0)
<activity android:name="com.something.something.abc" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.ABC" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="something.package.something.xyz" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.XYZ" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
只是一个例子尝试......