1. Download our Official Android App: Forums for Android!

Apps App Crash onClick

Discussion in 'Android Development' started by KarneeKarnay, Apr 14, 2016.

  1. KarneeKarnay

    KarneeKarnay Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    24
    Joined:
    Dec 3, 2012

    Dec 3, 2012
    24
    0
    15
    I'm creating an app that goes through JSON data on a website and then presents it to the user. It asks the user to enter a game name and searches through the JSON database to get the results. I'm having some problems with getting the results though. It seems like certain results crash the app when I click on them, but I don't get any errors.

    Code (Java):
    1.  
    2.  
    3. import android.content.Intent;
    4. import android.graphics.Bitmap;
    5. import android.os.AsyncTask;
    6. import android.support.v7.app.AppCompatActivity;
    7. import android.os.Bundle;
    8. import android.util.Log;
    9. import android.view.View;
    10. import android.widget.AdapterView;
    11. import android.widget.Button;
    12. import android.widget.EditText;
    13. import android.widget.ImageView;
    14. import android.widget.ListView;
    15. import android.widget.ProgressBar;
    16. import android.widget.TextView;
    17. import org.json.JSONArray;
    18. import org.json.JSONException;
    19. import org.json.JSONObject;
    20. import java.io.BufferedReader;
    21. import java.io.ByteArrayOutputStream;
    22. import java.io.IOException;
    23. import java.io.InputStreamReader;
    24. import java.net.HttpURLConnection;
    25. import java.net.URL;
    26. import java.net.URLEncoder;
    27. import java.util.ArrayList;
    28.  
    29. public class MainActivity extends AppCompatActivity {
    30.  
    31.     private Button button = null;
    32.     private EditText text = null;
    33.     private ProgressBar progressBar = null;
    34.     private TextView responseView = null;
    35.     private ListView gameListRows = null;
    36.     ImageView imageView = null;
    37.  
    38.     private String query_text = "";
    39.     @Override
    40.     protected void onCreate(Bundle savedInstanceState) {
    41.         super.onCreate(savedInstanceState);
    42.         setContentView(R.layout.activity_main);
    43.  
    44.         button = (Button) findViewById(R.id.s_button);
    45.         text = (EditText) findViewById(R.id.s_text);
    46.         progressBar = (ProgressBar) findViewById(R.id.progressBar);
    47.         responseView = (TextView) findViewById(R.id.responseView);
    48.         gameListRows = (ListView) findViewById(R.id.listView);
    49.         imageView = (ImageView) findViewById(R.id.icon);
    50.  
    51.         button.setOnClickListener(new View.OnClickListener() {
    52.             public void onClick(View v) {
    53.  
    54.                 query_text = text.getText().toString();
    55.                 new JSONParser().execute();
    56.             }
    57.         });
    58.     }
    59.  
    60.     public class JSONParser extends AsyncTask<Void,Void,String> {
    61.  
    62.         private Exception exception;
    63.         private GetJSONValues jsonValues = new GetJSONValues();
    64.         private ArrayList<Game> gameData = jsonValues.gameData;
    65.  
    66.         protected void onPreExecute()
    67.         {
    68.             progressBar.setVisibility(View.VISIBLE);
    69.             responseView.setText("");
    70.         }
    71.  
    72.         protected String doInBackground(Void... urls)
    73.         {
    74.             try {
    75.  
    76.                 URL url = new URL("http://www.giantbomb.com/api/search/?query=" + URLEncoder.encode(query_text, "UTF-8")
    77.                         + "&field_list=name,id,deck,description,image&resources=game,concept&api_key=API_KEY&format=json&offset=0");
    78.                 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    79.  
    80.                 try
    81.                 {
    82.                     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    83.  
    84.                     StringBuilder stringBuilder = new StringBuilder();
    85.                     String line;
    86.  
    87.                     while ((line = bufferedReader.readLine()) != null)
    88.                     {
    89.                         stringBuilder.append(line).append("\n");
    90.                     }
    91.                     bufferedReader.close();
    92.  
    93.                     String result = stringBuilder.toString();
    94.  
    95.                     try {
    96.                         JSONObject jsonObject = new JSONObject(result);
    97.                         JSONArray jsonArray = jsonObject.getJSONArray("results");
    98.                         jsonValues.getGameList(jsonArray);
    99.                         jsonValues.getValues();
    100.                     }
    101.                     catch (JSONException e)
    102.                     {
    103.                         e.printStackTrace();
    104.                     }
    105.                     return  result;
    106.                 }
    107.                 finally {
    108.                     urlConnection.disconnect();
    109.                 }
    110.             }
    111.             catch (IOException e)
    112.             {
    113.                 e.printStackTrace();
    114.                 return null;
    115.             }
    116.  
    117.         }
    118.  
    119.         protected void onPostExecute(String response)
    120.         {
    121.             if(response == null)
    122.             {
    123.                 response = "THERE WAS AN ERROR";
    124.             }
    125.             progressBar.setVisibility(View.GONE);
    126.             //Log.i("INFO", response);
    127.             RowAdapter adapter = new RowAdapter(MainActivity.this, gameData);
    128.             gameListRows.setAdapter(adapter);
    129.  
    130.             gameListRows.setOnItemClickListener(
    131.                     new AdapterView.OnItemClickListener()
    132.                     {
    133.                         @Override
    134.                         public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    135.                         {
    136.                             try {
    137.                                 Game gameInfo = gameData.get(position);
    138.                                 Bitmap temp = gameInfo.imageUrl;
    139.                                 ByteArrayOutputStream stream = new ByteArrayOutputStream();
    140.                                 gameInfo.imageUrl.compress(Bitmap.CompressFormat.PNG, 100, stream);
    141.                                 byte[] bytes = stream.toByteArray();
    142.  
    143.                                 gameInfo.imageUrl = null;
    144.                                 Intent myIntent = new Intent(view.getContext(), game_info.class);
    145.                                 myIntent.putExtra("tempGame", gameInfo);
    146.                                 myIntent.putExtra("gameImage", bytes);
    147.                                 startActivity(myIntent);
    148.                                 gameInfo.imageUrl = temp;
    149.                             }catch (Exception e)
    150.                             {
    151.                                 System.out.println(e);
    152.                             }
    153.                         }
    154.                     }
    155.  
    156.             );
    157.         }
    158.     }
    159. }
    160.  
    161.  
    162.  
    163.  
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,307
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,307
    10,413
    1,988
    Male
    Software developer
    South West of England
    If the app crashed you will have a stack trace in the Logcat output. Next time it crashes, please post this stack trace.
     
  3. KarneeKarnay

    KarneeKarnay Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    24
    Joined:
    Dec 3, 2012

    Dec 3, 2012
    24
    0
    15
    Here you go. Any ideas.

    Code (Text):
    1.  
    2. 04-14 11:06:58.459 14949-14949/? I/art: Not late-enabling -Xcheck:jni (already on)
    3. 04-14 11:06:58.495 14949-14949/com.temp.temp.gamelibary W/ActivityThread: Application com.temp.temp.gamelibary is waiting for the debugger on port 8100...
    4. 04-14 11:06:58.538 14949-14955/com.temp.temp.gamelibary I/art: Debugger is active
    5. 04-14 11:06:58.553 14949-14949/com.temp.temp.gamelibary W/System: ClassLoader referenced unknown path: /data/app/com.temp.temp.gamelibary-2/lib/x86
    6. 04-14 11:06:58.737 14949-14972/com.temp.temp.gamelibary D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
    7. 04-14 11:06:58.827 14949-14972/com.temp.temp.gamelibary I/OpenGLRenderer: Initialized EGL, version 1.4
    8. 04-14 11:06:58.877 14949-14972/com.temp.temp.gamelibary W/EGL_emulation: eglSurfaceAttrib not implemented
    9. 04-14 11:06:58.877 14949-14972/com.temp.temp.gamelibary W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb4034380, error=EGL_SUCCESS
    10. 04-14 11:06:59.535 14949-14949/com.temp.temp.gamelibary I/Choreographer: Skipped 44 frames!  The application may be doing too much work on its main thread.
    11.  
    12.  
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,307
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,307
    10,413
    1,988
    Male
    Software developer
    South West of England
    That's not a stack trace
     
  5. KarneeKarnay

    KarneeKarnay Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    24
    Joined:
    Dec 3, 2012

    Dec 3, 2012
    24
    0
    15
    Aplogise. I'm using android studio. Any idea where I can find that?
     
  6. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,307
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,307
    10,413
    1,988
    Male
    Software developer
    South West of England
  7. KarneeKarnay

    KarneeKarnay Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    24
    Joined:
    Dec 3, 2012

    Dec 3, 2012
    24
    0
    15
    Sorry for the delay. I've had some hardware problems.

    Heres the LogCat output. I can't see much from it.

     
  8. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,307
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,307
    10,413
    1,988
    Male
    Software developer
    South West of England
    There's still no stack trace.

    What actually happens with your app? Does it crash?
     

Share This Page

Loading...