TextView.settext()不起作用,Button.settext()效果很好

时间:2013-01-16 20:29:38

标签: android button textview settext

我一直在使用Runtime.exec并获取其输出的类,但是它不是很稳定...现在我使用这个类来获取命令“ls / sdcard /”的输出(知道我在我的Manifest中拥有正确的权限)...(我将在下面编写代码)我的问题是命令的Text输出可以显示为Toast,它可以设置为按钮的文本,但不能TextView的文本!当我运行TextView.settext()时,TextView根本没有显示任何文字!更奇怪的是,如果我使用像settext()这样的TextView.settext("Hey there , I am reset");它可以工作并显示文本!以下是代码:

MainActivity.class /的 Button.setOnClickListener

B1.setOnClickListener(new View.OnClickListener() {          
    public void onClick(View arg0) {
        String sdcard = File.separator+Environment.getExternalStorageDirectory().toString()+File.separator; //unused
        String[] arr = {"ls" , "/sdcard/..ROMS/"};//unused - was for experimentation
        String r = shell.sendSingleCommand("ls /sdcard/"); //"r" should now hold the output (stdin and stderr) of the command - shell is defined as Shells shell = new Shells();
        B3.setText(r); //Text of the button changes to the String r
        text.setText(r); //Text is blank , no text is shown at all , not even what it originally was - "text" is my TextView ... Here's my problem
        Show(r); // a method that shows a Toast for a given String - shows the String r normally in a normal Toast ...

    }
});

MainActivity中大多数变量的声明:

        final Button B1 = (Button) findViewById(R.id.button1);
        final Button B2 = (Button) findViewById(R.id.button2);
        final Button B3 = (Button) findViewById(R.id.button3);
        final SharedPreferences pref = getSharedPreferences("seaskyways.testingproject",0);     
        final TextView text = (TextView) findViewById(R.id.testing_text_view);
        final Shells shell = Shells.Shells(); //which returns new Shells()

布局/ activity_main.xml中:

<ScrollView xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="37dp"
            android:text="Button 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="52dp"
            android:text="Button 2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button1"
            android:layout_centerHorizontal="true"
            android:text="Button 3" />

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/button3" >

            <TextView
                android:id="@+id/testing_text_view"
                android:layout_width="222dp"
                android:layout_height="236dp"
                android:gravity="center|top"
                android:padding="10sp"
                android:text="Hey there !"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/abs__primary_text_holo_dark" />

        </ScrollView>
</RelativeLayout>
</ScrollView>

显示()

public void Show(String a){
    Toast.makeText(getApplicationContext(),a , Toast.LENGTH_SHORT).show();
}

Shells.class /的 sendSingleCommand()

public String sendSingleCommand(String singlecmd) { 
    String a = "";
    final BufferedReader in ;
    final BufferedReader er;

    final String r;

        try {
            p = Runtime.getRuntime().exec(singlecmd);           
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    er = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    try{
        String n;

        while((n = er.readLine())!=null){
            a = a + n + "\n";
        }

        while((n=in.readLine())!=null){
            a = a +"\n"+ n;
            while((n = er.readLine())!=null){
                a = a +"\n"+ n;
            }
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    p.destroy();
    try {
        in.close();
        er.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    final StringBuilder build = new StringBuilder();
    build.append(a);
    if(a.startsWith("null")){
        build.substring(4);
    }
    a= build.toString();
    return a;
}

AndroidManifest.xml(需要):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="seaskyways.testingproject"
    android:versionCode="1"
    android:versionName="beta" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
    <uses-permission 
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        />

    <application
        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >
        <activity
            android:name="seaskyways.testingproject.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Holo" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="seaskyways.testingproject.MenuActivity"
            android:label="@string/title_activity_menu" >
        </activity>
        <activity
            android:name="seaskyways.testingproject.Splash"
            android:label="@string/title_activity_splash" android:theme="@style/Theme.Sherlock.Dialog" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="seaskyways.testingproject.HorizontalScrollView"
            android:label="@string/title_activity_splash" >
            <intent-filter>
                <action android:name="android.intent.action.HSV" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果您需要在评论中了解任何内容,请

1 个答案:

答案 0 :(得分:1)

ScrollView只能托管一个直接孩子

尝试使用此布局: 更改为您使用的名称并尝试使用。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="138dp" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="104dp"
                android:text="Button" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="43dp"
                android:text="Button" />

            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:layout_marginTop="50dp" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />

                </LinearLayout>
            </ScrollView>

        </RelativeLayout>

    </LinearLayout>


</ScrollView>