说明: 我实现了一个navigationDrawer。其中myFirstFragment在我的homeFragment中意味着,我有viewpager和listview.I已经完全加载了我的数据和数据。
当我点击listview的一行时,它会调用一个类名GetValue,并在GetValue.I中传递一个意图来打开一个活动,即ScoreCard。
在记分卡中,我已经使用viewpager实现了一个tablayout,所以我在ScoreCard.Fragment中有多个片段,如ONE,TWO,THREE等。
My All从服务器加载数据的片段。
一切正常但问题是当我点击列表视图中设置到我的home_fragment中的行时,它加载了记分卡并且记分卡加载了片段。
在加载片段期间,我按下设备的后退按钮,不幸的是应用程序崩溃了。
这里发布了一些代码,这些代码在按下位于homeFragment上的listview行之后有效。
这是我的homeFragment,我在其中调用和适配器并将值设置为listview。
ScheduleAdapter CmAdapter = new ScheduleAdapter(getContext(), arr_completed, lst_key2);
CmAdapter.notifyDataSetChanged();
completed_listview.setAdapter(CmAdapter);
这是ScheduleAdapter.java
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
match_key = arr_matchKey.get(position);
Log.e("Schedule complted key", match_key);
MainActivity activity = (MainActivity) context;
GetValue gv = new GetValue(context);
gv.setFullUrl(match_key);
gv.setAccessToken(activity.getMyData());
gv.execute();
}
});
这是我的GetValue.java类,它从服务器获取数据并称为ScoreCard活动
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(st_overview.equals("abandoned") || st_overview.equals("canceled"))
{
DialogAbandoned ad=new DialogAbandoned(context);
ad.setDialog(short_name,msg_info+".");
}
else {
Intent intent = new Intent(context, ScoreCard.class);
intent.putExtra("tabvalue", tabVlaue);
intent.putExtra("teamName", name_of_team);
intent.putExtra("tabData", tabData);
intent.putExtra("match_key", match_key);
intent.putExtra("access_token", accessToken);
intent.putExtra("team_names", teams_name);
intent.putExtra("inn_len", inn_len);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
这是我的ScoreCard.java活动,其中包含带有viewpager的tablayout
public class ScoreCard extends AppCompatActivity {
Toolbar toolbar;
public ViewPager viewPager;
public TabLayout tabLayout;
String[] team_name = new String[10];
String[] runs = new String[4];
String[] wicket = new String[4];
String[] name_of_team_split;
public String[] parts, parts1;
public String name_of_team = "";
public String tab_value = "";
public String match_key = "";
public String tabData = "";
public int tabSelector;
public String access_token = "";
public String header_team_name = "";
int inn_len = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score_card);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Intent i = getIntent();
tab_value = i.getStringExtra("tabvalue");
tabData = i.getStringExtra("tabData");
name_of_team = i.getStringExtra("teamName");
inn_len = i.getIntExtra("inn_len", 0);
match_key = i.getStringExtra("match_key");
access_token = i.getStringExtra("access_token");
header_team_name = i.getStringExtra("team_names");
if (header_team_name != null && header_team_name != "") {
getSupportActionBar().setTitle(header_team_name);
}
try {
parts1 = tabData.split("`");
int k = 0;
for (int a = 0; a < parts1.length; a++, k++) {
try {
JSONObject obj = new JSONObject(parts1[a]);
runs[k] = obj.getString("runs");
wicket[k] = obj.getString("wickets");
} catch (JSONException e) {
e.printStackTrace();
}
}
name_of_team_split = name_of_team.split("=");
for (int b = 0; b < name_of_team_split.length; b++) {
JSONObject team1 = new JSONObject(name_of_team_split[b]);
team_name[b] = team1.getString("short_name");
}
} catch (JSONException e) {
e.printStackTrace();
}
setTabValue(tab_value);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
if (tabSelector == 1) {
viewPager.setCurrentItem(0, false);
} else if (tabSelector == 2) {
viewPager.setCurrentItem(1, false);
} else if (tabSelector == 3) {
viewPager.setCurrentItem(2, false);
} else if (tabSelector == 4) {
viewPager.setCurrentItem(4, false);
}
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
super.onBackPressed();
} else {
getFragmentManager().popBackStack();
}
}
private void setupTabIcons() {
if (tabSelector == 1) {
tabLayout.getTabAt(0).setText(team_name[0] + "-" + runs[0] + "/" + wicket[0]);
} else if (tabSelector == 2) {
tabLayout.getTabAt(0).setText(team_name[0] + "-" + runs[0] + "/" + wicket[0]);
tabLayout.getTabAt(1).setText(team_name[1] + "-" + runs[1] + "/" + wicket[1]);
} else if (tabSelector == 3) {
tabLayout.getTabAt(0).setText(team_name[0] + "-" + runs[0] + "/" + wicket[0]);
tabLayout.getTabAt(1).setText(team_name[1] + "-" + runs[1] + "/" + wicket[1]);
tabLayout.getTabAt(2).setText(team_name[2] + "-" + runs[2] + "/" + wicket[2]);
} else if (tabSelector == 4) {
tabLayout.getTabAt(0).setText(team_name[0] + "\n" + runs[0] + "/" + wicket[0]);
tabLayout.getTabAt(1).setText(team_name[1] + "\n" + runs[1] + "/" + wicket[1]);
tabLayout.getTabAt(2).setText(team_name[2] + "\n" + runs[2] + "/" + wicket[2]);
tabLayout.getTabAt(3).setText(team_name[3] + "\n" + runs[3] + "/" + wicket[3]);
}
}
public void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
if (tabSelector == 1) {
adapter.addFragment(new OneFragment());
} else if (tabSelector == 2) {
adapter.addFragment(new OneFragment());
adapter.addFragment(new TwoFragment());
} else if (tabSelector == 3) {
adapter.addFragment(new OneFragment());
adapter.addFragment(new TwoFragment());
adapter.addFragment(new ThreeFragment());
} else if (tabSelector == 4) {
adapter.addFragment(new OneFragment());
adapter.addFragment(new TwoFragment());
adapter.addFragment(new ThreeFragment());
adapter.addFragment(new FourFragment());
}
viewPager.setAdapter(adapter);
}
public void setTabValue(String tabs) {
parts = tabs.split(",");
int len = parts.length;
if (len == 1) {
tabSelector = 1;
} else if (len == 2) {
tabSelector = 2;
} else if (len == 3) {
tabSelector = 3;
} else if (len == 4) {
tabSelector = 4;
}
}
public String setInningScore() {
return tab_value;
}
public String geturl() {
return match_key;
}
public String GetAccessToken() {
return access_token;
}
public int setInningLength() {
return inn_len;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if(menu!=null){
menu.findItem(R.id.Score).setVisible(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
}
return super.onOptionsItemSelected(item);
}
}
这是我从记分卡调用的OneFragent。我只根据我的问题发布了所需的代码
adapter = new FullScoreAdapter(getContext(), p_name, p_key);
adapter.notifyDataSetChanged();
lst.setAdapter(adapter);
Helper.getListViewSize(lst);
上面的适配器调用FullScoreAdapter并将数据设置为OneFragment的列表视图
这是我的FullScoreAdapter
public class FullScoreAdapter extends BaseAdapter {
private Context context;
private List<String> batting_order;
private List<JSONObject> player_key;
int size=0;
String batting_ord="";
public static LayoutInflater inflater=null;
public FullScoreAdapter(Context context,List<String> batting_order,List<JSONObject> player_key){
this.context=context;
this.batting_order=batting_order;
if (batting_order != null && !batting_order.isEmpty()) {
size = batting_order.size();
}
this.player_key=player_key;
}
@Override
public int getCount() {
return this.batting_order.size();
}
@Override
public Object getItem(int position) {
return batting_order.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView txtname;
TextView txtballs;
TextView txtRuns;
TextView txtsixs;
TextView txtfours;
TextView txtout;
ImageView strike_ball;
LinearLayout out_layout_hide;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder= new Holder();
Typeface tf;
String fullname="",Balls="",Runs="",sixes="",fours="";
if(convertView==null) {
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//Here i got an error like java.lang.nullpointerException
convertView = inflater.inflate(R.layout.list_rows, null);
tf=Typeface.createFromAsset(convertView.getResources().getAssets(), "Roboto-Regular.ttf");
holder.txtname = (TextView) convertView.findViewById(R.id.txtName);
holder.txtname.setTypeface(tf,Typeface.BOLD);
holder.txtballs = (TextView) convertView.findViewById(R.id.txtBalls);
holder.txtballs.setTypeface(tf);
holder.txtRuns=(TextView)convertView.findViewById(R.id.txtRuns);
holder.txtRuns.setTypeface(tf);
holder.txtsixs=(TextView)convertView.findViewById(R.id.txtSixs);
holder.txtsixs.setTypeface(tf);
holder.txtfours=(TextView)convertView.findViewById(R.id.txtFours);
holder.txtfours.setTypeface(tf);
holder.txtout=(TextView)convertView.findViewById(R.id.out);
holder.txtout.setTypeface(tf);
holder.strike_ball=(ImageView)convertView.findViewById(R.id.strike_ball);
holder.out_layout_hide=(LinearLayout)convertView.findViewById(R.id.out_layout_color);
convertView.setTag(holder);
try {
JSONObject obj = this.player_key.get(position);
batting_ord=this.batting_order.get(position);
fullname=obj.getString("fullname");
JSONObject match=obj.getJSONObject("match");
JSONObject bats_innings=match.getJSONObject("innings");
JSONObject first_inning=bats_innings.getJSONObject("1");
JSONObject first_batting=first_inning.getJSONObject("batting");
if(first_batting.isNull("balls")) {
if(first_batting.isNull("runs")){
Runs="0";
}
else{
Runs=first_batting.getString("runs");
}
Balls="0";
sixes="0";
fours="0";
}
else{
Balls=first_batting.getString("balls");
Runs=first_batting.getString("runs");
sixes=first_batting.getString("sixes");
fours=first_batting.getString("fours");
}
try{
if(first_batting.has("out_str")){
String out=first_batting.getString("out_str");
holder.txtout.setText(out);
holder.strike_ball.setVisibility(View.GONE);
}
else{
holder.strike_ball.setVisibility(View.VISIBLE);
holder.txtout.setText("not out");
}
}
catch (JSONException e)
{
e.printStackTrace();
}
holder.txtname.setText(fullname);
holder.txtRuns.setText(Runs);
holder.txtballs.setText(Balls);
holder.txtfours.setText(fours);
holder.txtsixs.setText(sixes);
}
catch (JSONException e){
e.printStackTrace();
}
int setcolor=position;
if(setcolor%2==0){
convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
holder.out_layout_hide.setBackgroundColor(Color.parseColor("#EFF5F5"));
}
else{
convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
holder.out_layout_hide.setBackgroundColor(Color.parseColor("#EFF5F5"));
}
}
else {
holder = (Holder) convertView.getTag();
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent player_intent=new Intent(context, PlayerIccStats.class);
player_intent.putExtra("player_key",batting_order.get(position));
context.startActivity(player_intent);
}
});
return convertView;
}
}
当我按下后退按钮时,我的标签被加载时出错。
FATAL EXCEPTION: main
java.lang.NullPointerException
at adapter.FullScoreAdapter.getView(FullScoreAdapter.java:78)
at comman.Helper.getListViewSize(Helper.java:20)
at fragments.OneFragment$JSONLoader.onPostExecute(OneFragment.java:593)
at fragments.OneFragment$JSONLoader.onPostExecute(OneFragment.java:300)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4950)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
请帮我解决这个问题。