在我的应用程序中,我创建了一个 TabView,并且正在使用 AsyncTask 加载 JSON 数据。我正在使用 onTabChangedListner(),当单击特定选项卡时,AsyncTask 运行并加载 JSON 数据。当我单击该特定选项卡时,我最终得到了 NullPointerException,但我无法弄清楚我的应用程序从哪里得到 NullPointerException。
The following is the code of TabGroups Class:
public class TabGroups extends ActivityGroup {
ArrayList<String> list;
Window window;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(list == null)
list = new ArrayList<String>();
}
@Override
public void finishFromChild(Activity child)
{
LocalActivityManager manager = getLocalActivityManager();
int index = list.size() -1;
if(index < 1)
{
finish();
return;
}
manager.destroyActivity(list.get(index), true);
list.remove(index);
index--;
String lastId = list.get(index);
Intent in = manager.getActivity(lastId).getIntent();
window = manager.startActivity(lastId, in);
setContentView(window.getDecorView());
}
public void startChildActivity(String Id, Intent intent)
{
window = getLocalActivityManager().startActivity(Id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if(window != null)
{
list.add(Id);
setContentView(window.getDecorView());
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
return true;
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
@Override
public void onBackPressed()
{
int length = list.size();
if(length > 1)
{
Activity current = getLocalActivityManager().getActivity(list.get(length - 1));
current.finish();
}
}
}
The following is the code of TabView Class:
public class TabView extends TabActivity implements OnTabChangeListener {
private static TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("home").setIndicator("Home").setContent(new Intent(this, TabGroup1.class)));
tabHost.addTab(tabHost.newTabSpec("top5").setIndicator("My Top5").setContent(new Intent(this, TabGroup2.class)));
tabHost.addTab(tabHost.newTabSpec("resv").setIndicator("My Reservation").setContent(new Intent(this, TabGroup3.class)));
tabHost.addTab(tabHost.newTabSpec("srch").setIndicator("Search").setContent(new Intent(this, TabGroup4.class)));
tabHost.addTab(tabHost.newTabSpec("prfl").setIndicator("Profile").setContent(new Intent(this, TabGroup5.class)));
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(this);
}
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
Log.v("tag", String.valueOf(tabId));
if(tabId.equals("home"))
new LoadingScreen(this).execute("login");
}
}
The following is the code of TabGroup1 Class
public class TabGroup1 extends TabGroups {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
startChildActivity("Home", new Intent(this, Home.class));
}
}
The following is the code of LoadingScreen Class:
public class LoadingScreen extends AsyncTask <String, String, String> {
private ProgressDialog progress;
private Context context;
private static String rqst, message;
private static final String TAG_MESAG = "message";
public static ArrayList<HashMap<String, String>> top5List = new ArrayList<HashMap<String, String>>();
public LoadingScreen(Context context)
{
this.context = context;
progress = new ProgressDialog(context);
progress.setMessage("Loading...");
progress.setIndeterminate(false);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setCancelable(true);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progress.show();
}
@Override
protected String doInBackground(String... data) {
// TODO Auto-generated method stub
rqst = data[0];
if(rqst.equals("login"))
{
String url = "http://mygogolfteetime.com/iphone/login/ramu@gmail.com/123456";
JsonParser jParser = new JsonParser();
JSONObject json = jParser.getJSONfromUrl(url);
try
{
message = json.getString(TAG_MESAG);
}
catch(JSONException e)
{
Log.v("Log", String.valueOf(e));
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
progress.dismiss();
super.onPostExecute(result);
}
}
The following is the code of JsonParser:
public class JsonParser {
static InputStream is;
static JSONObject jObj;
static String json = null;
public JSONObject getJSONfromUrl(String url)
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
jObj = new JSONObject(json);
}
catch(JSONException e)
{
e.printStackTrace();
}
return jObj;
}
}
JsonParser 在其他应用程序中运行良好,但在此应用程序中无法正常工作
Home 类只包含 onCreate() 而没有别的
The Errors that I am getting are:
08-29 08:21:39.021: E/AndroidRuntime(4156): FATAL EXCEPTION: AsyncTask #1
08-29 08:21:39.021: E/AndroidRuntime(4156): java.lang.RuntimeException: An error occured while executing doInBackground()
08-29 08:21:39.021: E/AndroidRuntime(4156): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-29 08:21:39.021: E/AndroidRuntime(4156): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-29 08:21:39.021: E/AndroidRuntime(4156): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-29 08:21:39.021: E/AndroidRuntime(4156): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-29 08:21:39.021: E/AndroidRuntime(4156): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-29 08:21:39.021: E/AndroidRuntime(4156): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-29 08:21:39.021: E/AndroidRuntime(4156): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-29 08:21:39.021: E/AndroidRuntime(4156): at java.lang.Thread.run(Thread.java:1096)
08-29 08:21:39.021: E/AndroidRuntime(4156): Caused by: java.lang.NullPointerException
08-29 08:21:39.021: E/AndroidRuntime(4156): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
08-29 08:21:39.021: E/AndroidRuntime(4156): at org.json.JSONTokener.nextValue(JSONTokener.java:90)
08-29 08:21:39.021: E/AndroidRuntime(4156): at org.json.JSONObject.<init>(JSONObject.java:154)
08-29 08:21:39.021: E/AndroidRuntime(4156): at org.json.JSONObject.<init>(JSONObject.java:171)
08-29 08:21:39.021: E/AndroidRuntime(4156): at com.pnf.gogolf.JsonParser.getJSONfromUrl(JsonParser.java:65)
08-29 08:21:39.021: E/AndroidRuntime(4156): at com.pnf.gogolf.LoadingScreen.doInBackground(LoadingScreen.java:48)
08-29 08:21:39.021: E/AndroidRuntime(4156): at com.pnf.gogolf.LoadingScreen.doInBackground(LoadingScreen.java:1)
08-29 08:21:39.021: E/AndroidRuntime(4156): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-29 08:21:39.021: E/AndroidRuntime(4156): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-29 08:21:39.021: E/AndroidRuntime(4156): ... 4 more
08-29 08:21:40.052: E/WindowManager(4156): Activity com.pnf.gogolf.TabView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44eba938 that was originally added here
08-29 08:21:40.052: E/WindowManager(4156): android.view.WindowLeaked: Activity com.pnf.gogolf.TabView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44eba938 that was originally added here
08-29 08:21:40.052: E/WindowManager(4156): at android.view.ViewRoot.<init>(ViewRoot.java:247)
08-29 08:21:40.052: E/WindowManager(4156): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-29 08:21:40.052: E/WindowManager(4156): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-29 08:21:40.052: E/WindowManager(4156): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-29 08:21:40.052: E/WindowManager(4156): at android.app.Dialog.show(Dialog.java:241)
08-29 08:21:40.052: E/WindowManager(4156): at com.pnf.gogolf.LoadingScreen.onPreExecute(LoadingScreen.java:37)
08-29 08:21:40.052: E/WindowManager(4156): at android.os.AsyncTask.execute(AsyncTask.java:391)
08-29 08:21:40.052: E/WindowManager(4156): at com.pnf.gogolf.TabView.onTabChanged(TabView.java:34)
08-29 08:21:40.052: E/WindowManager(4156): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:356)
08-29 08:21:40.052: E/WindowManager(4156): at android.widget.TabHost.setCurrentTab(TabHost.java:341)
08-29 08:21:40.052: E/WindowManager(4156): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
08-29 08:21:40.052: E/WindowManager(4156): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
08-29 08:21:40.052: E/WindowManager(4156): at android.view.View.performClick(View.java:2408)
08-29 08:21:40.052: E/WindowManager(4156): at android.view.View$PerformClick.run(View.java:8816)
08-29 08:21:40.052: E/WindowManager(4156): at android.os.Handler.handleCallback(Handler.java:587)
08-29 08:21:40.052: E/WindowManager(4156): at android.os.Handler.dispatchMessage(Handler.java:92)
08-29 08:21:40.052: E/WindowManager(4156): at android.os.Looper.loop(Looper.java:123)
08-29 08:21:40.052: E/WindowManager(4156): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-29 08:21:40.052: E/WindowManager(4156): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 08:21:40.052: E/WindowManager(4156): at java.lang.reflect.Method.invoke(Method.java:521)
08-29 08:21:40.052: E/WindowManager(4156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-29 08:21:40.052: E/WindowManager(4156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-29 08:21:40.052: E/WindowManager(4156): at dalvik.system.NativeStart.main(Native Method)
请帮助我,因为我必须在 Tabs 的帮助下重新设计我的应用程序..
提前致谢。