安卓开发大神来看下intent的问题
app装到手机上,只要一点击listview列表,程序就会自动关闭,大概可以知道是listview监听后调用的start函数有问题。start()函数定义在最下面求大神看...
app装到手机上,只要一点击listview列表,程序就会自动关闭,大概可以知道是listview监听后调用的start函数有问题。start()函数定义在最下面求大神看下问题出在哪儿,跳转所写的AAA.class为最简单的布局文件
包名就不复制了
public class MainActivity extends Activity {
private ListView lv;
ArrayList name;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView)findViewById(R.id.textView1);
//ListView mylistview = (ListView)findViewById(R.id.listView1);
lv = (ListView) findViewById(R.id.listView1);
name = new ArrayList();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
String result = arg0.getItemAtPosition(arg2).toString();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
textView.setText(result);
start();
}
});
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//File path = Environment.getExternalStorageDirectory();// 获得SD卡路径
File path = new File("/mnt/extSdCard/qqmusic/");
File[] files = path.listFiles();// 读取
getFileName(files);
}
SimpleAdapter adapter = new SimpleAdapter(this, name, R.layout.item,new String[] { "Name" }, new int[] { R.id.textView1 });
lv.setAdapter(adapter);
for (int i = 0; i < name.size(); i++) {
Log.i("INDEX", "list. name: " + name.get(i));
}
}
private void getFileName(File[] files) {
if (files != null) {// 先判断目录是否为空,否则会报空指针
for (File file : files) {
if (file.isDirectory()) {
Log.i("INDEX", "若是文件目录。继续读1" + file.getName().toString() + file.getPath().toString());
getFileName(file.listFiles());
Log.i("INDEX", "若是文件目录。继续读2" + file.getName().toString() + file.getPath().toString());
} else {
String fileName = file.getName();
if (fileName.endsWith(".mp3")) {
HashMap map = new HashMap();
String s = fileName.substring(0,fileName.lastIndexOf(".")).toString();
Log.i("INDEX",s);
map.put("Name", fileName.substring(0,fileName.lastIndexOf(".")));
name.add(map);
}
}
}
}
}
void start(){
Intent intent = new Intent(MainActivity.this,AAA.class);
intent.setClass(this, AAA.class);
startActivity(intent);
}
} 展开
包名就不复制了
public class MainActivity extends Activity {
private ListView lv;
ArrayList name;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView)findViewById(R.id.textView1);
//ListView mylistview = (ListView)findViewById(R.id.listView1);
lv = (ListView) findViewById(R.id.listView1);
name = new ArrayList();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
String result = arg0.getItemAtPosition(arg2).toString();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
textView.setText(result);
start();
}
});
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//File path = Environment.getExternalStorageDirectory();// 获得SD卡路径
File path = new File("/mnt/extSdCard/qqmusic/");
File[] files = path.listFiles();// 读取
getFileName(files);
}
SimpleAdapter adapter = new SimpleAdapter(this, name, R.layout.item,new String[] { "Name" }, new int[] { R.id.textView1 });
lv.setAdapter(adapter);
for (int i = 0; i < name.size(); i++) {
Log.i("INDEX", "list. name: " + name.get(i));
}
}
private void getFileName(File[] files) {
if (files != null) {// 先判断目录是否为空,否则会报空指针
for (File file : files) {
if (file.isDirectory()) {
Log.i("INDEX", "若是文件目录。继续读1" + file.getName().toString() + file.getPath().toString());
getFileName(file.listFiles());
Log.i("INDEX", "若是文件目录。继续读2" + file.getName().toString() + file.getPath().toString());
} else {
String fileName = file.getName();
if (fileName.endsWith(".mp3")) {
HashMap map = new HashMap();
String s = fileName.substring(0,fileName.lastIndexOf(".")).toString();
Log.i("INDEX",s);
map.put("Name", fileName.substring(0,fileName.lastIndexOf(".")));
name.add(map);
}
}
}
}
}
void start(){
Intent intent = new Intent(MainActivity.this,AAA.class);
intent.setClass(this, AAA.class);
startActivity(intent);
}
} 展开
3个回答
展开全部
您好
直接index=positio就可以了,在android的listview中,你点击的时候会自动获取position的
.
直接index=positio就可以了,在android的listview中,你点击的时候会自动获取position的
.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要么去掉 Intent intent = new Intent(MainActivity.this,AAA.class);括号中MainActivity.this,AAA.class
要么去掉intent.setClass(this, AAA.class);
要么去掉intent.setClass(this, AAA.class);
追问
还是老样子
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
那你看看LogCat打印的信息
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询