简单的Android程序错误

时间:2017-03-24 07:49:16

标签: android

package com.example.enable_apps;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends Activity implements Runnable{

    private Runtime run;
    private String what_is_going_on="Program Started:\n";
    private TextView t;

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

        t=(TextView) findViewById(R.id.textView1);
        t.setText(what_is_going_on);

        run=Runtime.getRuntime();
        Thread t1= new Thread(this);
        t1.start();       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void run() {   
        PackageManager pm = getPackageManager();          
        List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

        for (ApplicationInfo i  : packages){
            String current_package = i.packageName;
            try {
                what_is_going_on+="\nenabling package: "+current_package;
                Log.v("string", what_is_going_on);
                runOnUiThread(new Runnable(){
                    public void run(){
                        t.invalidate();
                    }});                

                run.exec("su | pm enable "+current_package).waitFor();
                Log.v("enabled: ",i.packageName);
            } catch (IOException e) {
                what_is_going_on+="\nerror package: "+current_package+": "+e.getMessage().toString();
                Log.v("string", what_is_going_on);
                runOnUiThread(new Runnable(){
                    public void run(){
                        t.invalidate();
                    }});
                Log.v("error: ",e.getMessage().toString());
            } catch (InterruptedException e) {
                what_is_going_on+="\nerror package: "+current_package+": "+e.getMessage().toString();
                Log.v("string", what_is_going_on);
                runOnUiThread(new Runnable(){
                    public void run(){
                        t.invalidate();
                    }});
                Log.v("error: ",e.getMessage().toString());
            }
        }
    }
}

所以我正在清理我的硬盘,我找到了这个程序并运行它,但屏幕上没有输出,它只显示程序启动: 在控制台中它正在启用应用程序,所以为什么会发生这种情况?

这是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="wrap_content"
    android:gravity="top"
    tools:context="com.example.enable_apps.MainActivity" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

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

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:text="TextView" />
        </LinearLayout>
    </ScrollView>  
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

  

屏幕上没有输出,它只显示程序开始:

不,输出已显示。您看到这是因为您的DT setText带有&#34;程序已启动:&#34;

尝试为textView添加一些背景颜色,您会看到TextView

更改textView

中的文字

替换

textView

private String what_is_going_on="Program Started:\n";

private String what_is_going_on="Hello Worldenabling package: androidenabling package: android.googleSearch.googleSearchWidget ";

中添加所有句子