setText()在android中不起作用

setText()在android中不起作用,android,Android,下面是我的代码。在这里,我只获取所有正在运行的进程的列表,并获取它们的各种属性,如进程名称、进程id,然后将进程id存储到数组列表中。然后,我使用trafficstats()找到了所有进程ID使用的数据流量。现在,我想在TextView中显示这些值。但是,mu代码没有显示这些值。 有人能告诉我我做错了什么吗 我在activity_main.xml中使用了3个TextView。首先TextView显示了我的值,但另2个不起作用。 整个代码都在MainActivity.java类中 TextView

下面是我的代码。在这里,我只获取所有正在运行的进程的列表,并获取它们的各种属性,如进程名称、进程id,然后将进程id存储到
数组列表中。然后,我使用
trafficstats()
找到了所有进程ID使用的数据流量。现在,我想在
TextView
中显示这些值。但是,mu代码没有显示这些值。 有人能告诉我我做错了什么吗

我在
activity_main.xml
中使用了3个
TextView
。首先
TextView
显示了我的值,但另2个不起作用。 整个代码都在
MainActivity.java
类中

TextView textView1 = (TextView) findViewById(R.id.textView1);
//this.setContentView(textView1);    
    ActivityManager actvityManager = (ActivityManager)
        this.getSystemService( ACTIVITY_SERVICE );
        List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
        ArrayList<Integer> a1 = new ArrayList<Integer>();
        for(int i = 0; i < procInfos.size(); i++)
        {
                textView1.setText(textView1.getText().toString()+procInfos.get(i).processName+" "+procInfos.get(i).pid+ 
                            " " + String.valueOf(procInfos.get(i).processName.length())+"\n");
                //if(procInfos.get(i).processName.equals("com.android.camera")) {
                    //Toast.makeText(getApplicationContext(), "Camera App is running", Toast.LENGTH_LONG).show();
                //}
                a1.add(procInfos.get(i).pid);

        }

        for(Integer a2 : a1){
            long re = TrafficStats.getUidRxBytes(a2);
            long sd = TrafficStats.getUidTxBytes(a2);

            arr1.add(a2);

            System.out.println("Recieved Bytes: "+re/1000 + "Send Bytes: "+sd/1000);
            TextView textView2 = (TextView) findViewById(R.id.textView2);
            TextView textView3 = (TextView) findViewById(R.id.textView3);

            textView2.setText(textView2.getText().toString()+String.valueOf(re));
            textView3.setText("ABAABABBA");

        }
TextView textView1=(TextView)findViewById(R.id.textView1);
//这个.setContentView(textView1);
ActivityManager actvityManager=(ActivityManager)
这个.getSystemService(活动\服务);
List procInfos=actvityManager.getrunningappprocesss();
ArrayList a1=新的ArrayList();
对于(int i=0;i
以下是activity_main.xml类:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="14dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="98dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="93dp"
    android:text="TextView" />

这是我的整个MainActivity.java类:-

public class MainActivity extends Activity {

static ArrayList<Integer> arr1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    TextView textView1 = (TextView) findViewById(R.id.textView1);

    //this.setContentView(textView1);        


    ActivityManager actvityManager = (ActivityManager)
            this.getSystemService( ACTIVITY_SERVICE );
            List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
            ArrayList<Integer> a1 = new ArrayList<Integer>();
            for(int i = 0; i < procInfos.size(); i++)
            {
                    textView1.setText(textView1.getText().toString()+procInfos.get(i).processName+" "+procInfos.get(i).pid+ 
                                " " + String.valueOf(procInfos.get(i).processName.length())+"\n");
                    //if(procInfos.get(i).processName.equals("com.android.camera")) {
                        //Toast.makeText(getApplicationContext(), "Camera App is running", Toast.LENGTH_LONG).show();
                    //}
                    a1.add(procInfos.get(i).pid);


            }

            TextView textView2 = (TextView) findViewById(R.id.textView2);
            TextView textView3 = (TextView) findViewById(R.id.textView3);
            for(Integer a2 : a1){
                long re = TrafficStats.getUidRxBytes(a2);
                long sd = TrafficStats.getUidTxBytes(a2);

                //arr1.add(a2);

                System.out.println("Recieved Bytes: "+re/1000 + "Send Bytes: "+sd/1000);


                textView2.append(""+Long.toString(re));
                textView3.append("ABAABABBA");
                textView3.invalidate();

            }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
公共类MainActivity扩展活动{
静态数组列表arr1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView1=(TextView)findViewById(R.id.textView1);
//这个.setContentView(textView1);
ActivityManager actvityManager=(ActivityManager)
这个.getSystemService(活动\服务);
List procInfos=actvityManager.getrunningappprocesss();
ArrayList a1=新的ArrayList();
对于(int i=0;i
}

谢谢所有给我建议的人。我的问题现在解决了。
感谢大家的快速响应。

您需要在每次
setText(“”
之后调用方法
invalidate()
,以查看UI中的更改

示例:-

TextView text1.setText("dummy text");
text1.invalidate();
我希望它有助于移动

TextView textView2 = (TextView) findViewById(R.id.textView2);
TextView textView3 = (TextView) findViewById(R.id.textView3);
在您的
for循环之外
。无需在每次迭代中使用
findViewById()

顺便说一句,在Android中使用System.out.println()比使用System.out.println()更好。

试试以下方法:

TextView textView2 = (TextView) findViewById(R.id.textView2);
TextView textView3 = (TextView) findViewById(R.id.textView3);       
for(Integer a2 : a1){
    long re = TrafficStats.getUidRxBytes(a2);
    long sd = TrafficStats.getUidTxBytes(a2);
    arr1.add(a2);
    System.out.println("Recieved Bytes: "+re/1000 + "Send Bytes: "+sd/1000);
    textView2.setText(textView2.getText().toString()+Long.toString(re));
    textView3.setText("ABAABABBA");

}

试试这个,在循环的每个新迭代中,您都在创建新的textview,所以在循环之外初始化这两个textview

TextView textView2 = (TextView) findViewById(R.id.textView2);
TextView textView3=(TextView)findViewById(R.id.textView3)

这可能会有帮助

TextView textView2 = (TextView) findViewById(R.id.textView2);
TextView textView3 = (TextView) findViewById(R.id.textView3);
不在正确的位置,请将它们移到第一行。
TextView textView1=(TextView)findViewById(R.id.textView1)

感谢大家的快速回复。 实际上,所有的文本视图都彼此相距很远,这就是为什么我不能看到所有的值,而且我使用了

textView3.append(Long.toString(sd)+"KB"); 

显示值。

您是否可以打印此日志。。。System.out.println(“接收字节:+re/1000+”发送字节:+sd/1000);对这工作得很好。请尝试以下
textView2.setText(String.valueOf(re))在for语句上添加所有值,并在for之后设置为TextView。您确定这不是布局问题吗?也许textView2和textView3只是折叠或不可见。你能发布activity_main.xml吗?我也尝试了这个。但是,仍然没有得到TextView中的值,我使用了它。但问题仍然存在。在textView3中甚至无法获取文本。