TextView不显示.但在logcat中显示时,一切都很好



我试图在文本视图中显示输出。但无法显示它。我不知道它是否显示在我的程序中包com.iiitb.nikhil.sindhu;

 import java.io.BufferedReader; 
 import java.io.DataInput;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
 import android.widget.TextView;
  import java.util.*;
 import android.util.*;
   public class LinuxShellCommandAndroidProgramActivity extends Activity {
/** Called when the activity is first created. */
TextView tv;
public void onCreate(Bundle savedInstanceState) {
     tv=new TextView(this);
    super.onCreate(savedInstanceState);
    try {
        Process process = Runtime.getRuntime().exec("/system/bin/su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        DataInputStream is=new DataInputStream(process.getInputStream());
        //os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("/system/bin/chmod 600 /data/data/newfilen");
        os.writeBytes("cd /data/data/dalvik-cache/n");
        os.writeBytes("/system/bin/ls -l  /data/dalvik-cachen");
        String output=new String();
        tv.setText("hii n");
        //setContentView(tv);
        String temp = new String();
           while((output=is.readLine())!=null)
           {
              Log.i("Check ",output);
              tv.append(output);
           }
            setContentView(tv);
            os.flush();
       } catch (IOException e) {
            // TODO Auto-generated catch block
             e.printStackTrace();
     }

   }
 }

在Textview上设置LayoutParameter,或者只是在main.xml文件中使用布局,并通过layout.addView(textview);在该布局中添加Textview

调用superClass后取tv=new TextView(this);像这样

    super.onCreate(savedInstanceState);
    tv=new TextView(this);

您必须更改为

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     tv=(TextView) findViewById(R.id.tv);  
....
然后打开res/layout/main.xml在linerlayout标签 之间插入此代码
<TextView android:text="Text box" android:layout_width="60dp" android:id="@+id/tv" android:layout_height="wrap_content"></TextView>

你可以通过这个方法放置textview

ScrollView sv = new ScrollView(this);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);
    TextView tv = new TextView(this);
    tv.setText("Dynamic layouts ftw!");
    ll.addView(tv);
    this.setContentView(sv);

相关内容

  • 没有找到相关文章

最新更新