setText()在android中不起作用

时间:2014-02-27 09:14:40

标签: android

以下是我的代码。在此,我只获取所有正在运行的进程的列表,并获取其各种属性,如进程名称,进程ID和我将进程ID存储到ArrayList。然后,我使用trafficstats()找到了所有流程ID使用的数据流量。现在,我想在TextView中显示这些值。但是,mu代码没有显示这些值。 任何人都可以建议我,我做错了吗?

我在TextView中使用3 activity_main.xml。首先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");

        }

这是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;
}

}

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

6 个答案:

答案 0 :(得分:0)

您需要在每个invalidate()之后调用方法setText("")以查看用户界面中的更改

示例: -

TextView text1.setText("dummy text");
text1.invalidate();

我希望它有所帮助

答案 1 :(得分:0)

移动

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

for loop之外。无需在每次迭代中使用findViewById()

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

答案 2 :(得分:0)

试试这个:

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");

}

答案 3 :(得分:0)

尝试这一点,在每个新的循环迭代中,您正在创建新的textview,因此在循环之外初始化两个文本视图

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

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

它可能会有所帮助

答案 4 :(得分:0)

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

不在正确的位置,只需将它们移动到第一行即可。     TextView textView1 =(TextView)findViewById(R.id.textView1);

答案 5 :(得分:0)

感谢大家的快速回复。 实际上,所有的textView都放在彼此远的地方,这就是为什么我无法看到所有的值而且我也用过了

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

显示值。