kkxiaoxian
2017-04-11 02:07
采纳率: 62.5%
浏览 2.0k

关于安卓页面跳转的问题

做了两个页面与页面跳转按钮,二页面想使用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" >







  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • harveyXXX 2017-04-12 06:39
    已采纳

    之前遇到过类似问题。Activity02 如果继承ListActivity, 那么加载的layout中ListView id需要为@id/android:list。

    已采纳该答案
    打赏 评论
  • ziyejinwei1994 2017-04-11 04:00

    恕我愚钝,我没找到你的listview....是不是你没有写?

    打赏 评论
  • zhtcooler 2017-04-11 07:08

    Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

    你需要一个listview在Activity02中 并且 id 标明为android.R.id.list

    打赏 评论
  • ThomasZhangTY97 2017-04-11 08:12

    坐拿赏金图片说明

    打赏 评论
  • fcwxin 2017-04-11 08:50

    ListActivity的默认布局由一个位于屏幕中心的全屏列表构成。如果你不想使用默认的布局,可以在onCreate()方法中通过setContentView()方法设定你自己的布局。如果指定你自己定制的布局,你的布局中必须包含一个id为"@id/android:list"的ListView。 若你还指定了一个id为"@id/android:empty"的view,当ListView中没有数据要显示时,这个view就会被显示,同 时 ListView会被隐藏。
    <
    LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingLeft="8dp"

    android:paddingRight="8dp">

    android:id="@+id/android:title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="The following is a list:" />

    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FF00"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

    android:id="@id/android:empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#FF0000"
    android:text="No data"
    android:gravity="center_vertical|center_horizontal" />


    是不是这个问题

    打赏 评论
  • RoidCoder 2017-04-11 13:47

    java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list',ListView的id必须是使用系统指定的
    android.R.id.list

    打赏 评论
  • 码农_凉城 2017-04-12 08:07

    ID没找到 先去找下ID 看下名字是否重复了 重复改之, 或者 根本就没有定义这个ID 。

    打赏 评论

相关推荐 更多相似问题