因此,我正在开发一个从服务器获取故事的应用程序。故事分为几部分。我想将每个故事部分放在不同的ViewPager页面上,但是到目前为止,我尝试过的操作要么使应用程序崩溃,要么给我一个空白屏幕。
这是我用于该活动的代码:
package com.firegaming.myfirstapp;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Parcelable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ContinueStoryActivity extends FragmentActivity {
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static int partCounter;
private static String storyID;
ArrayList<String> storyPartList = new ArrayList<>();
FragmentStatePagerAdapter adapter;
ViewPager pager;
JSONArray parts = null;
private static final String GET_STORY_URL = "http://firegaming.host56.com/get_story.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private static final String TAG_PARTS = "parts";
private static final String TAG_PARTCOUNT = "partCount";
private static final String TAG_STORYID = "storyID";
private static final String TAG_STORYTEXT = "storyText";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_continue_story);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
new GetStory().execute();
}
@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_continue_story, 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);
}
public class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
return StoryFragment.newInstance(storyPartList.get(pos).toString());
}
@Override
public int getCount() {
return storyPartList.size();
}
public int getItemPosition(Object object){
return POSITION_NONE;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
//do nothing here! no call to super.restoreState(arg0, arg1);
}
}
class GetStory extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(ContinueStoryActivity.this);
pDialog.setMessage("Getting Story...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args){
int success;
try{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", MainActivity.USERNAME));
Log.d("Request!", "Starting!");
JSONObject json = jsonParser.makeHttpRequest(GET_STORY_URL, "GET", params);
Log.d("Story Attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1){
parts = json.getJSONArray(TAG_PARTS);
partCounter = Integer.parseInt(json.getString(TAG_PARTCOUNT));
storyID = json.getString(TAG_STORYID);
return json.getString(TAG_SUCCESS);
} else {
Log.d("Story Get Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException ex){
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
pDialog.dismiss();
if (file_url != null) {
try {
for (int i = 0; i < parts.length(); i++) {
JSONObject c = parts.getJSONObject(i);
int id = c.getInt(TAG_STORYID);
String storyText = c.getString(TAG_STORYTEXT);
storyPartList.add(id-1, storyText);
}
} catch (JSONException ex) {
ex.printStackTrace();
}
adapter.notifyDataSetChanged();
}
}
}
}
我在这里做错了什么?
编辑:
如建议的那样,当不返回null时,应用再次崩溃。
这些都是应用崩溃时logcat中出现的所有错误。
01-03 12:29:19.298 29861-29861/com.firegaming.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.firegaming.myfirstapp, PID: 29861
java.lang.NullPointerException
at com.firegaming.myfirstapp.ContinueStoryActivity$MyPagerAdapter.getItem(ContinueStoryActivity.java:88)
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:105)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
at android.support.v4.view.ViewPager.populate(ViewPager.java:987)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:447)
at com.firegaming.myfirstapp.ContinueStoryActivity$GetStory.onPostExecute(ContinueStoryActivity.java:163)
at com.firegaming.myfirstapp.ContinueStoryActivity$GetStory.onPostExecute(ContinueStoryActivity.java:99)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
最佳答案
我有一些建议:
我对storyPartList是hasmap的事实感到怀疑,其id是输入数据。如果传入的ID例如为[0,2,3],则getItem(1)将失败。如果让storyPartLists仅作为一个数组列表会发生什么(正如名称暗示的那样)?
适配器的getCount
同样由传入数据决定:
@Override
public int getCount() {
return partCounter;
}
相反,让计数器取决于
storyPartList
的实际大小:@Override
public int getCount() {
return storyPartList.size();
}
现在,让寻呼机数据完全由添加到
storyPartList
的数据量确定,我建议在onCreate()
中创建一次寻呼机,而不是每次添加故事时都重新创建它:ViewPager pager;
FragmentStatePagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_continue_story);
new GetStory().execute();
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager())
pager.setAdapter(adapter);
}
现在在
onPostExecute
中只需执行以下操作:adapter.notifyDataSetChanged();
这将使适配器根据
storyPartList
的内容刷新其内容。为了使适配器正确更新,您可能必须将其添加到MyPagerAdapter
:public int getItemPosition(Object object){
return POSITION_NONE;
}
建议摘要
使storyPartList成为列表而不是Map
让getCount返回storyPartList.size()
在onCreate中创建寻呼机,并在添加故事时调用notifyDataSetChanged
最后在适配器中实现getItemPosition以使其正确更新