Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行时活动完全为空-可能是由于internet权限?_Java_Android - Fatal编程技术网

Java 运行时活动完全为空-可能是由于internet权限?

Java 运行时活动完全为空-可能是由于internet权限?,java,android,Java,Android,我对java/android开发相当陌生,所以请耐心听我说 我创建了一个应用程序,作为在线课程的一部分,应该运行一个“猜名人游戏” 问题是,当我运行代码时,主活动完全为空 奇怪的是,课程中介绍的应用程序运行正常,我逐字逐句地遵循代码。我设法找到了另一位候选人,他也有同样的问题,但无法解决 日志中没有错误(我可以找到),我似乎找不到任何类似问题的帖子 只有在清单xml中添加以下权限时,才会出现这种奇怪的行为: <uses-permission android:name="android.pe

我对java/android开发相当陌生,所以请耐心听我说

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

问题是,当我运行代码时,主活动完全为空

奇怪的是,课程中介绍的应用程序运行正常,我逐字逐句地遵循代码。我设法找到了另一位候选人,他也有同样的问题,但无法解决

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

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

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

任何帮助都将不胜感激,谢谢

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

.get()
不要这样做是问题的原因还是建议?很可能两者都是。该方法在处理任何内容之前等待任务返回,以便冻结UI。你几乎从来都不想使用它。你能给我一些建议,如何以更好的方式达到同样的效果吗?讨论它。您想使用
onPostExecute()
将结果分配给所需的变量。它在这里执行:“result=task.execute(“;”
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