簡體   English   中英

將數據從片段傳遞到活動到第二個活動時出錯

[英]Error when passing data from fragment to activity to a second activity

我有點想出了如何從片段->活動->第二個片段發送數據。 但是,當第二個片段嘗試提取數據時,它將遇到空錯誤。 首先顯示數據的代碼如下所示:

Intent i = new Intent(getActivity(), scoring_two.class);
i.putExtra("Cv",Cv);
dicstr_twotank_frag.this.startActivity(i);

scoring_two活動的代碼如下:

package edu.UDayton.www;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

//This is simply the placeholder activity for allowing the fragment to attach to something.
//Fragements deal with screen rotations more easily than activities, which is why all the 
//code is the fragment scoring.  But fragments can't exist by themselves so there needs to
//be a dummy activity to hold them.
public class scoring_two extends FragmentActivity{


@Override
protected void onSaveInstanceState(Bundle saveInstanceState) {
    super.onSaveInstanceState(saveInstanceState);
}
@Override
protected void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    setContentView(R.layout.rel5);
    Intent intent = getIntent();
    Double Cv = intent.getDoubleExtra("Cv", 1.0);
    Fragment scoring = new Fragment();
    Bundle bundle = new Bundle();
    bundle.putDouble("Cv", Cv);
    scoring.setArguments(bundle);
  }
}

得分片段:

package edu.UDayton.www;
public class scoring extends Fragment {
  public TextView textOverallScore;
  Bundle bundle;
  Double Cv;
  //Button declarations
  Button main;
  //This creates a view that the fragment will use to obtain the actual layout for the activity
public View rootView;
  //This is the beginning of activity initialization.  Since this is an Android fragment, the first
  //step is to attach it to an actual activity.  The activity in this case is essentially a placeholder.
  //All of the real work of this system comes from this Android fragment
  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
  }
//This code preserves certain values if you exit the app and come back to it
@Override
public void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    //This retains all of the data in the app upon screen rotation.  Normally the activity is destroyed and
    //re-created upon rotation.  This prevents this from happening.
    setRetainInstance(true);
}
//These lines obtain the layout view "scoring" to use for the fragment.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
    View rootView = getActivity().getLayoutInflater().inflate(R.layout.scoring, null);
    //Identify the text that needs to change
    textOverallScore=(TextView)rootView.findViewById(R.id.textOverallScore);
    //Receives data
    bundle = getArguments();
    Cv = bundle.getDouble("Cv", 1.0);
    textOverallScore.setText(Cv+"");
    //Identifies the button
    main = (Button) rootView.findViewById(R.id.buttonMain);
    //Sends user back to main screen
    main.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(getActivity(), systems.class);
            scoring.this.startActivity(i);
        }
    });
    return rootView;
}

}

我相信錯誤發生在下一行。

   Cv = bundle.getDouble("Cv", 1.0);

如果我注釋掉該行,並且僅使Cv = 0.0或其他值,它將對輸出文件進行適當的更改。 任何幫助,將不勝感激,謝謝!

在@dhun的幫助下,我將得分片段的代碼更新為以下代碼:

public class scoring extends Fragment {
public TextView textOverallScore;
//Button declarations
Button main;
//This creates a view that the fragment will use to obtain the actual layout for the activity
public View rootView;
//This is the beginning of activity initialization.  Since this is an Android fragment, the first
//step is to attach it to an actual activity.  The activity in this case is essentially a placeholder.
//All of the real work of this system comes from this Android fragment
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);}
public Bundle getBundle() {
    Bundle bundle = new Bundle();
    return bundle;
}
//This code preserves certain values if you exit the app and come back to it
@Override
public void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    //This retains all of the data in the app upon screen rotation.  Normally the activity is destroyed and
    //re-created upon rotation.  This prevents this from happening.
    setRetainInstance(true);
}
//These lines obtain the layout view "scoring" to use for the fragment.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
    View rootView = getActivity().getLayoutInflater().inflate(R.layout.scoring, null);
    //Identify the text that needs to change
    textOverallScore=(TextView)rootView.findViewById(R.id.textOverallScore);
    //Receives data
    Double Cv = getBundle().getDouble("Cv",1.36);
    textOverallScore.setText(Cv + "");
    //Identifies the button
    main = (Button) rootView.findViewById(R.id.buttonMain);
    //Sends user back to main screen
    main.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(getActivity(), systems.class);
            scoring.this.startActivity(i);
        }
    });


return rootView;
}

}

現在,它不再崩潰,而是僅傳遞最終的默認值,不接收Cv值(或找不到它)。

我認為您通過評分片段的onCreatView()獲得的包

  bundle = getArguments();

返回空包,這就是為什么在您提到的行上得到空指針異常的原因。 嘗試實現回調接口,以在活動和片段之間來回發送數據。 讓我給你一個很好的例子。 您應該參考此處發布的答案,然后像這樣編寫代碼並嘗試。

https://stackoverflow.com/a/16578326/4150528

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM