2016-12-05 65 views
-1

會有人告訴我需要在我的Java文件中添加哪些代碼,以便使我的應用程序能夠使用NetworkOnMainThreadException工作,因爲我真的很掙扎。當它開始說應用程序停止工作時,應用程序崩潰。我需要添加哪些代碼才能在手機上使用此應用程序?

package com.airrocketapps.matthillman.guessthecelebrity; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.Toast; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.Random; 
import java.util.concurrent.ExecutionException; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 


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.button); 
     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(); 
     } 




    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 
+0

您已經提到的''NetworkOnMainThreadException''。你試圖找出它的含義嗎? – f1sh

+0

是的,但有點在理解代碼的地方迷路了,我是一個初學者,所以任何幫助將不勝感激。 –

回答

0

在您的代碼中一切都看起來不錯。

請確保您的手機上有互聯網連接正在嘗試運行此應用程序。

如果您正在使用互聯網,那麼別忘了添加到您的AndroidManifest.xml

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

更多的幫助:https://stackoverflow.com/a/6343299/7004388

+0

我已經將此添加到清單中,感謝您的幫助。當你調用onCreate方法時,你看到任何可能導致應用程序崩潰的東西 –

相關問題