做了两个页面与页面跳转按钮,二页面想使用ListActivity,但是继承了ListActivity类后闪退
继承Activity时运行正常,但不能看到List效果,请大神帮忙看下代码,感谢
DEBUG
线程 [main](已暂挂(异常 RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) 行: 3254
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) 行: 3350
ActivityThread.access$1100(ActivityThread, ActivityThread$ActivityClientRecord, Intent) 行: 223
ActivityThread$H.handleMessage(Message) 行: 1794
ActivityThread$H(Handler).dispatchMessage(Message) 行: 102
Looper.loop() 行: 148
ActivityThread.main(String[]) 行: 7224
Method.invoke(Object, Object...) 行: 不可用 [本机方法]
ZygoteInit$MethodAndArgsCaller.run() 行: 1230
ZygoteInit.main(String[]) 行: 1120
报错日志
04-11 10:03:01.957: E/MotionRecognitionManager(30956): mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@b530bca
04-11 10:03:01.967: E/MotionRecognitionManager(30956): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@82fc13b
04-11 10:03:01.967: E/MotionRecognitionManager(30956): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@82fc13b
04-11 10:03:01.967: D/AndroidRuntime(30956): Shutting down VM
04-11 10:03:01.967: E/AndroidRuntime(30956): FATAL EXCEPTION: main
04-11 10:03:01.967: E/AndroidRuntime(30956): Process: com.example.audiorecord, PID: 30956
04-11 10:03:01.967: E/AndroidRuntime(30956): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.audiorecord/com.example.audiorecord.Activity02}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.app.ActivityThread.access$1100(ActivityThread.java:223)
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.os.Looper.loop(Looper.java:148)
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.app.ActivityThread.main(ActivityThread.java:7224)
04-11 10:03:01.967: E/AndroidRuntime(30956): at java.lang.reflect.Method.invoke(Native Method)
04-11 10:03:01.967: E/AndroidRuntime(30956): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-11 10:03:01.967: E/AndroidRuntime(30956): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-11 10:03:01.967: E/AndroidRuntime(30956): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-11 10:03:01.967: E/AndroidRuntime(30956): at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:479)
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.app.Activity.setContentView(Activity.java:2388)
04-11 10:03:01.967: E/AndroidRuntime(30956): at com.example.audiorecord.Activity02.onCreate(Activity02.java:38)
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.app.Activity.performCreate(Activity.java:6877)
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
04-11 10:03:01.967: E/AndroidRuntime(30956): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
04-11 10:03:01.967: E/AndroidRuntime(30956): ... 9 more
Activity1:
package com.example.audiorecord;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class RecordActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
/* 设置显示main.xml布局*/
setContentView(R.layout.main);
/* findViewById(R.id.button1)取得布局main.xml中的button1 /
Button button = (Button) findViewById(R.id.button1);
/ 监听button的事件信息*/
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) { /* 新建一个Intent对象*/
Intent intent = new Intent(); /* 指定intent要启动的类*/
intent.setClass(RecordActivity.this, Activity02.class); /* 启动一个新的Activity /
startActivity(intent); / 关闭当前的Activity */
RecordActivity.this.finish(); } });
}
}
Activity2
package com.example.audiorecord;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class Activity02 extends ListActivity{
//声明
private List items = null;//存放名称
private List paths = null;//存放路径
private String rootPath = "/";
private TextView tv;
private Button Back;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 设置显示main2布局*/
setContentView(R.layout.main2);
/* findViewById(R.id.button2)取得布局main.xml中的button2 */
/* 监听button的事件信息*/
tv = (TextView) this.findViewById(R.id.TextView);
this.getFileDir(rootPath);//获取rootPath目录下的文件.
Back = (Button) findViewById(R.id.back);
Back.setOnClickListener(new BackListener());
}
class BackListener implements OnClickListener
{
public void onClick(View v)
{
Intent intent = new Intent(); /* 指定intent要启动的类*/
intent.setClass(Activity02.this, RecordActivity.class); /* 启动一个新的Activity */
startActivity(intent); /* 关闭当前的Activity */
Activity02.this.finish();
}
}
public void getFileDir(String filePath) {
try{
this.tv.setText("当前路径:"+filePath);// 设置当前所在路径
items = new ArrayList();
paths = new ArrayList();
File f = new File(filePath);
File[] files = f.listFiles();// 列出所有文件
// 如果不是根目录,则列出返回根目录和上一目录选项
if (!filePath.equals(rootPath)) {
items.add("返回根目录");
paths.add(rootPath);
items.add("返回上一层目录");
paths.add(f.getParent());
}
// 将所有文件存入list中
if(files != null){
int count = files.length;// 文件个数
for (int i = 0; i < count; i++) {
File file = files[i];
items.add(file.getName());
paths.add(file.getPath());
}
}
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, items);
this.setListAdapter(adapter);
}catch(Exception ex){
ex.printStackTrace();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String path = paths.get(position);
File file = new File(path);
//如果是文件夹就继续分解
if(file.isDirectory()){
this.getFileDir(path);
}else{
new AlertDialog.Builder(this).setTitle("提示").setMessage(file.getName()+" 是一个文件!").setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}}
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
package="com.example.audiorecord"
android:versionCode="1"
android:versionName="1.0" >
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:name=".RecordActivity"
android:label="@string/app_name" >