运行时活动完全空白 - 可能是因为互联网权限?

时间:2016-08-21 20:46:51

标签: java android

我是java / android开发的新手,所以请耐心等待。

我已经创建了一个应用程序作为在线课程的一部分,据说应该运行“猜猜名人游戏”。

问题是当我运行我的代码时,主要活动是完全空白的。

奇怪的是,课程中提供的应用程序运行没有问题,我已逐字逐句地遵循代码。我找到了另外一个有同样问题且无法解决问题的候选人。

日志中没有错误(我可以找到),我似乎找不到任何类似问题的帖子。

当我在清单xml中添加以下权限时,这种奇怪的行为才会出现:

<uses-permission android:name="android.permission.INTERNET" />

模拟器的屏幕截图(也在真实设备上测试):

https://s9.postimg.org/su0gc58gf/Capture.png/

MainActivity的代码:

public class MainActivity extends Activity {

ArrayList<String> celebURLs = new ArrayList<String>();
ArrayList<String> celebNames = new ArrayList<String>();
int chosenCeleb = 0;
int locationOfCorrectAnswer = 0;
String[] answers = new String[4];

ImageView imageView;
Button button0;
Button button1;
Button button2;
Button button3;

public void celebChosen(View view) {

    if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))) {

        Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_LONG).show();

    } else {

        Toast.makeText(getApplicationContext(), "Wrong! It was " + celebNames.get(chosenCeleb), Toast.LENGTH_LONG).show();

    }

    createNewQuestion();

}

public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {


    @Override
    protected Bitmap doInBackground(String... urls) {

        try {

            URL url = new URL(urls[0]);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.connect();

            InputStream inputStream = connection.getInputStream();

            Bitmap myBitmap = BitmapFactory.decodeStream(inputStream);

            return myBitmap;


        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return null;
    }
}


public class DownloadTask extends AsyncTask<String, Void, String> {


    @Override
    protected String doInBackground(String... urls) {

        String result = "";
        URL url;
        HttpURLConnection urlConnection = null;

        try {

            url = new URL(urls[0]);

            urlConnection = (HttpURLConnection) url.openConnection();

            InputStream in = urlConnection.getInputStream();

            InputStreamReader reader = new InputStreamReader(in);

            int data = reader.read();

            while (data != -1) {

                char current = (char) data;

                result += current;

                data = reader.read();
            }

            return result;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;
    }
}

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

    imageView = (ImageView) findViewById(R.id.imageView);
    button0 = (Button) findViewById(R.id.button1);
    button1 = (Button) findViewById(R.id.button2);
    button2 = (Button) findViewById(R.id.button3);
    button3 = (Button) findViewById(R.id.button4);

    DownloadTask task = new DownloadTask();
    String result = null;

    try {

        result = task.execute("http://www.posh24.com/celebrities").get();

        String[] splitResult = result.split("<div class=\"sidebarContainer\">");

        Pattern p = Pattern.compile("<img src=\"(.*?)\"");
        Matcher m = p.matcher(splitResult[0]);

        while (m.find()) {

            celebURLs.add(m.group(1));

        }

        p = Pattern.compile("alt=\"(.*?)\"");
        m = p.matcher(splitResult[0]);

        while (m.find()) {

            celebNames.add(m.group(1));

        }


    } catch (InterruptedException e) {

        e.printStackTrace();

    } catch (ExecutionException e) {

        e.printStackTrace();

    }

    createNewQuestion();

}

public void createNewQuestion() {

    Random random = new Random();
    chosenCeleb = random.nextInt(celebURLs.size());

    ImageDownloader imageTask = new ImageDownloader();

    Bitmap celebImage;

    try {

        celebImage = imageTask.execute(celebURLs.get(chosenCeleb)).get();

        imageView.setImageBitmap(celebImage);

        locationOfCorrectAnswer = random.nextInt(4);

        int incorrectAnswerLocation;

        for (int i = 0; i < 4; i++) {

            if (i == locationOfCorrectAnswer) {

                answers[i] = celebNames.get(chosenCeleb);

            } else {

                incorrectAnswerLocation = random.nextInt(celebURLs.size());

                while (incorrectAnswerLocation == chosenCeleb) {

                    incorrectAnswerLocation = random.nextInt(celebURLs.size());

                }

                answers[i] = celebNames.get(incorrectAnswerLocation);


            }


        }

        button0.setText(answers[0]);
        button1.setText(answers[1]);
        button2.setText(answers[2]);
        button3.setText(answers[3]);


    } catch (Exception e) {
        e.printStackTrace();
    }


}

}

日志:

  08-21 20:47:46.778 835-835/? I/Choreographer: Skipped 37 frames!  The application may be doing too much work on its main thread.
08-21 20:47:49.283 421-519/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client

                                           --------- beginning of system
08-21 20:47:49.335 421-434/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.shenstone.guessthecelebrity2/.MainActivity (has extras)} from uid 10007 on display 0
08-21 20:47:49.614 421-433/? I/art: Background sticky concurrent mark sweep GC freed 10530(678KB) AllocSpace objects, 3(608KB) LOS objects, 12% free, 9MB/10MB, paused 4.358ms total 263.235ms
08-21 20:47:49.854 69-69/? I/art: Background concurrent mark sweep GC freed 794(33KB) AllocSpace objects, 0(0B) LOS objects, 90% free, 111KB/1135KB, paused 12.562ms total 112.418ms
08-21 20:47:49.945 421-1030/? I/ActivityManager: Start proc com.example.shenstone.guessthecelebrity2 for activity com.example.shenstone.guessthecelebrity2/.MainActivity: pid=3562 uid=10055 gids={50055, 9997, 3003} abi=armeabi-v7a
08-21 20:47:49.982 3562-3562/? I/art: Not late-enabling -Xcheck:jni (already on)
08-21 20:47:52.244 3562-3574/? W/art: Suspending all threads took: 12.799ms
08-21 20:47:52.262 3562-3574/? I/art: Background partial concurrent mark sweep GC freed 322(512KB) AllocSpace objects, 0(0B) LOS objects, 64% free, 555KB/1579KB, paused 14.923ms total 43.870ms
08-21 20:47:52.395 3562-3574/? I/art: Background sticky concurrent mark sweep GC freed 219(436KB) AllocSpace objects, 0(0B) LOS objects, 28% free, 1129KB/1579KB, paused 9.677ms total 22.436ms
08-21 20:47:52.534 3562-3574/? I/art: WaitForGcToComplete blocked for 10.763ms for cause Background
08-21 20:47:53.258 3562-3574/? I/art: Background sticky concurrent mark sweep GC freed 469(1634KB) AllocSpace objects, 0(0B) LOS objects, 58% free, 660KB/1599KB, paused 6.830ms total 31.639ms
08-21 20:47:56.495 3562-3573/? I/art: WaitForGcToComplete blocked for 21.032ms for cause HeapTrim
08-21 20:47:59.381 421-443/? W/ActivityManager: Launch timeout has expired, giving up wake lock!
08-21 20:48:00.154 3562-3574/? W/art: Suspending all threads took: 39.137ms
08-21 20:48:00.183 3562-3574/? I/art: Background partial concurrent mark sweep GC freed 108(3KB) AllocSpace objects, 57(862KB) LOS objects, 60% free, 663KB/1687KB, paused 41.100ms total 174.221ms
08-21 20:48:00.244 3562-3574/? W/art: Suspending all threads took: 10.311ms
08-21 20:48:00.255 3562-3574/? I/art: Background sticky concurrent mark sweep GC freed 53(1872B) AllocSpace objects, 30(451KB) LOS objects, 56% free, 728KB/1687KB, paused 12.216ms total 26.132ms
08-21 20:48:00.354 3562-3574/? W/art: Suspending all threads took: 9.038ms
08-21 20:48:00.415 3562-3574/? I/art: Background partial concurrent mark sweep GC freed 135(4KB) AllocSpace objects, 81(1221KB) LOS objects, 48% free, 1094KB/2MB, paused 12.238ms total 111.401ms

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

您正在使用AsyncTask但未启动它。使用execute()方法启动异步任务。