I am attempting to get an on progress display on the screen while a response is being formulated but my application is crashing, I am new at android, but I do know that as I step through before I implemented the onprogress method it worked, I’m assuming because it got the response from server first and then I updated the display, but when I run i do not see a response from server on the application. Any help would be greatly appreciated.
logCat output when crashed:
12-20 13:01:30.636: D/dalvikvm(597): GC_CONCURRENT freed 189K, 4% free 10154K/10503K, paused 19ms+21ms
12-20 13:01:31.806: D/dalvikvm(597): GC_CONCURRENT freed 391K, 6% free 10278K/10823K, paused 27ms+20ms
12-20 13:01:31.876: D/dalvikvm(597): GC_FOR_ALLOC freed 370K, 6% free 10235K/10887K, paused 39ms
12-20 13:01:31.926: D/dalvikvm(597): GC_FOR_ALLOC freed 351K, 6% free 10242K/10887K, paused 41ms
12-20 13:01:31.976: D/dalvikvm(597): GC_FOR_ALLOC freed 319K, 6% free 10245K/10887K, paused 40ms
12-20 13:01:32.036: D/dalvikvm(597): GC_FOR_ALLOC freed 322K, 6% free 10243K/10887K, paused 45ms
12-20 13:01:32.156: D/AndroidRuntime(597): Shutting down VM
12-20 13:01:32.156: W/dalvikvm(597): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
12-20 13:01:32.176: E/AndroidRuntime(597): FATAL EXCEPTION: main
12-20 13:01:32.176: E/AndroidRuntime(597): java.lang.NullPointerException
12-20 13:01:32.176: E/AndroidRuntime(597): at android.app.Activity.findViewById(Activity.java:1794)
12-20 13:01:32.176: E/AndroidRuntime(597): at edu.byu.hummedia.ReadWebpageAsyncTask$DownloadWebPageTask.onProgressUpdate(ReadWebpageAsyncTask.java:147)
12-20 13:01:32.176: E/AndroidRuntime(597): at edu.byu.hummedia.ReadWebpageAsyncTask$DownloadWebPageTask.onProgressUpdate(ReadWebpageAsyncTask.java:1)
12-20 13:01:32.176: E/AndroidRuntime(597): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:618)
12-20 13:01:32.176: E/AndroidRuntime(597): at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 13:01:32.176: E/AndroidRuntime(597): at android.os.Looper.loop(Looper.java:137)
12-20 13:01:32.176: E/AndroidRuntime(597): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-20 13:01:32.176: E/AndroidRuntime(597): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 13:01:32.176: E/AndroidRuntime(597): at java.lang.reflect.Method.invoke(Method.java:511)
12-20 13:01:32.176: E/AndroidRuntime(597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-20 13:01:32.176: E/AndroidRuntime(597): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-20 13:01:32.176: E/AndroidRuntime(597): at dalvik.system.NativeStart.main(Native Method)
12-20 13:06:01.806: I/Process(597): Sending signal. PID: 597 SIG: 9
hummedia class:
package edu.byu.hummedia;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class Hummedia extends Activity implements ActionBar.TabListener {
/** Called when the activity is first created. */
ListView myListView = null;
public ArrayAdapter<String> adapter;
Button basicSubmitButton;
Button advancedSubmitButton;
EditText myBasicEditText;
EditText myAdvancedEditText;
EditText title;
EditText language;
EditText category;
EditText keyword;
ActionBar bar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bar = getActionBar();
bar.addTab(bar.newTab().setText("Basic\nSearch").setTabListener(this));
bar.addTab(bar.newTab().setText("Advanced\nSearch").setTabListener(this));
bar.addTab(bar.newTab().setText("Results").setTabListener(this));
//bar.addTab(bar.newTab().setText("Exit").setTabListener(this));
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowHomeEnabled(true);
// remove the activity title to make space for tabs
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayUseLogoEnabled(false);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
if (tab.getText().equals("Basic\nSearch"))
{
Toast.makeText(this, "Searching for: " + "basic" + "...", Toast.LENGTH_SHORT).show();
}
if (tab.getText().equals("Advanced\nSearch"))
{
Toast.makeText(this, "Searching for: " + "Advanced" + "...", Toast.LENGTH_SHORT).show();
}
if (tab.getText().equals("Results"))
{
Toast.makeText(this, "Searching for: " + "Results" + "...", Toast.LENGTH_SHORT).show();
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
if (tab.getText().equals("Basic\nSearch"))
{
setContentView(R.layout.basic);
basicSubmitButton = (Button)findViewById(R.id.myBasicSubmitbutton);
myBasicEditText = (EditText)findViewById(R.id.myBasicEditText);
}
if (tab.getText().equals("Advanced\nSearch"))
{
setContentView(R.layout.advanced);
advancedSubmitButton = (Button)findViewById(R.id.myAdvancedSubmitbutton);
title = (EditText)findViewById(R.id.title);
language = (EditText)findViewById(R.id.language);
category = (EditText)findViewById(R.id.category);
keyword = (EditText)findViewById(R.id.keyword);
//Toast.makeText(this, "Searching for: " + "Advanced" + "...", Toast.LENGTH_SHORT).show();
}
if (tab.getText().equals("Results"))
{
setContentView(R.layout.results);
//Toast.makeText(this, "Searching for: " + "Results" + "...", Toast.LENGTH_SHORT).show();
}
}
public void basicSubmitButton(View view) {
String query = myBasicEditText.getText().toString();
bar.setSelectedNavigationItem(2);
ReadWebpageAsyncTask basicSubmission = new ReadWebpageAsyncTask();
basicSubmission.query(query);
myListView = (ListView) findViewById (R.id.lv);
myListView.setTextFilterEnabled(true);
myListView.clearChoices();
adapter = new ArrayAdapter<String>(Hummedia.this,
android.R.layout.simple_list_item_1,
basicSubmission.mediaList);
myListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public void advancedSubmitButton(View view) {
String mytitle = title.getText().toString();
String mylanguage = language.getText().toString();
String mycategory = category.getText().toString();
String mykeyword = keyword.getText().toString();
String query="";
if (mytitle.isEmpty() != true)
{
query+="title="+mytitle;
}
if (mylanguage.isEmpty() != true)
{
query+="language="+mylanguage;
}
if (mycategory.isEmpty() != true)
{
query+="subject="+mycategory;
}
if (mykeyword.isEmpty() != true)
{
query+="keyword="+mykeyword;
}
//Toast.makeText(this, "I was called...", Toast.LENGTH_SHORT).show();
bar.setSelectedNavigationItem(2);
ReadWebpageAsyncTask advancedSubmission = new ReadWebpageAsyncTask();
advancedSubmission.advquery(query);
//bar.setSelectedNavigationItem(2);
myListView = (ListView) findViewById (R.id.lv);
myListView.setTextFilterEnabled(true);
myListView.clearChoices();
adapter = new ArrayAdapter<String>(Hummedia.this,
android.R.layout.simple_list_item_1,
advancedSubmission.mediaList);
myListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
Async task class:
package edu.byu.hummedia;
import java.io.BufferedReader;
import android.widget.Toast;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import android.view.View;
import android.widget.ProgressBar;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toast;
public class ReadWebpageAsyncTask extends ListActivity {
ProgressBar progressBar;
private TextView textView;
public ListView myListView = null;
List<String> mediaList = new ArrayList();
public ArrayAdapter<String> adapter;
String[] newArray;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
progressBar = (ProgressBar)findViewById(R.id.progressbar);
progressBar.setProgress(0);
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
newArray = new String[mediaList.size()];mediaList.toArray(newArray);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, newArray);
setListAdapter(adapter);
readWebpage(myListView);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG);
}
private class DownloadWebPageTask extends AsyncTask<String, Integer, String> {
int myProgress;
@Override
protected String doInBackground(String... urls) {
String response = "";
String responseBuilder;
String responseString = "";
JSONArray jsonArray;
JSONObject jObject = new JSONObject();
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
try {
jsonArray = new org.json.JSONArray(response);
jObject = jsonArray.toJSONObject(jsonArray);
for (int i=0;i<jObject.length();i++)
{
responseBuilder="\n";
responseBuilder+= "\n title: "+((JSONObject) jsonArray.getJSONObject(i).get("videoinfo")).get("title");
responseBuilder+= "\n subject: "+ ((JSONObject) jsonArray.getJSONObject(i).get("videoinfo")).get("subject");
responseBuilder+= "\n language: "+ ((JSONObject) jsonArray.getJSONObject(i).get("videoinfo")).get("language");
mediaList.add(responseBuilder);
responseString += responseBuilder;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(myProgress<100){
myProgress++;
publishProgress(myProgress);
SystemClock.sleep(100);
}
return responseString;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
// Toast.makeText(ReadWebpageAsyncTask.this,
// "onPreExecute", Toast.LENGTH_LONG).show();
myProgress = 0;
}
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
if (progressBar == null)
progressBar = (ProgressBar)findViewById(R.id.progressbar);
progressBar.setProgress(values[0]);
}
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
// Toast.makeText(ReadWebpageAsyncTask.this,
// "onPostExecute", Toast.LENGTH_LONG).show();
}
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://hummedia.byu.edu/mediainfo/search/?title=Harry" });
}
public void query(String query) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://hummedia.byu.edu/mediainfo/search/?keyword="+query});
}
public void advquery(String query) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://hummedia.byu.edu/mediainfo/search/?"+query});
}
}
The log you posted is telling you exactly what line in your code caused the problem. It looks like you’re not properly testing if your reference to your progressbar control succeeds. Change your code to: