2

我有以下代码:

package com.example.task;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainTask extends Activity {

private Button BprocessesShow;
private TextView processesShow;


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

    ActivityManager manager =       (ActivityManager)getSystemService(ACTIVITY_SERVICE);
     final List<ActivityManager.RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();


    BprocessesShow=(Button) this.findViewById(R.id.BshowProcesses);
    processesShow=(TextView) this.findViewById(R.id.showProcesses);     


    BprocessesShow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            processesShow.setText((CharSequence) runningProcesses);
        }
    });

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main_task, menu);
    return true;
}

}

好吧,我想做的是,当用户按下我制作的按钮时,我想向他/她展示他/她手机上所有正在运行的进程......当我在手机上运行此应用程序时,应用程序崩溃并且所有我收到一条消息不幸的是任务(那是我的名字的应用程序)已停止......任何帮助都会非常有用!

4

1 回答 1

1

在这一行

processesShow.setText((CharSequence) runningProcesses);

您正在将 List 转换为 CharSequence。尝试以下方式:

StringBuilder b = new StringBuilder();
for (ActivityManager.RunningAppProcessInfo process: runningProcesses) {
    b.append(processs.processName);
    b.append("\n");
}
processesShow.setText(b.toString());
于 2012-12-09T23:11:52.617 回答