我设法解决了如何使用Interface java类将信息从两个片段(片段1a和片段1b)发送到主要活动,但是我很难从主要活动发送信息到另一个片段(片段2)。我已经尝试了十几件事,但我仍然在主要活动和片段2中获得Null Pointer异常,但有以下几点:
主要活动:
//Send data to Even Split Fragment 2
evenSplit_CalculationFragment2.tellMeWhatEachPersonOwesES(eachPersonOwesESString);
片段2:
public void tellMeWhatEachPersonOwesES (String eachPersonOwesThisESString) {
amountEachPersonOwesES.setText(eachPersonOwesThisESString);
};
如果有人可以提供帮助,那将非常感激。下面的主要活动,片段2和Communicator界面的完整代码:
主要活动:
package apps.created.jay.splititapp;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements View.OnClickListener, Communicator {
Button nextButton, calculateButton, backButton;
partySizeAndMethodFragment partySize_MethodFragment;
evenSplitCalculationFragment1 evenSplit_CalculationFragment1;
evenSplitCalculationFragment2 evenSplit_CalculationFragment2;
byWhoHadWhatCalculationFragment1 byWhoHadWhat_CalculationFragment1;
byWhoHadWhatCalculationFragment2 byWhoHadWhat_CalculationFragment2;
String numberInPartyString;
String splitMethodString;
String billGrandTotalString;
String eachPersonOwesESString;
double numberInPartyDouble, eachPersonOwesESDouble, billGrandTotalDouble;
TextView codeTesterView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Implement Initial Fragment (i.e. Set Party Size and Method of Split)
FragmentManager splitItFragmentManager = getFragmentManager();
FragmentTransaction splitItFragmentTransaction = splitItFragmentManager.beginTransaction();
partySize_MethodFragment = new partySizeAndMethodFragment();
splitItFragmentTransaction.add(R.id.fragment_container, partySize_MethodFragment);
splitItFragmentTransaction.commit();
//Initialise Next Button
nextButton = (Button) findViewById(R.id.next1button);
nextButton.setOnClickListener(this);
//Initialise Calculate Button
calculateButton = (Button) findViewById(R.id.calculateButton);
calculateButton.setOnClickListener(this);
//Initialise Back Button
backButton = (Button) findViewById(R.id.backButton);
backButton.setOnClickListener(this);
}
//COMMUNICATIONS
//Method of Split (from Party Size & Method Fragment)
@Override
public void respond(String splitMethodData) {
splitMethodString = splitMethodData;
}
// Party Size (from Party Size & Method Fragment)
@Override
public void partyOfWhat(String numberInPartyData) {
numberInPartyString = numberInPartyData;
}
// Total Bill (from Even Split Calculation Fragment 1)
@Override
public void billGrandTotal(String billGrandTotalData) {
billGrandTotalString = billGrandTotalData;
}
//On Button Click
@Override
public void onClick(View v) {
FragmentManager splitItFragmentManager = getFragmentManager();
FragmentTransaction splitItFragmentTransaction = splitItFragmentManager.beginTransaction();
if(v.getId() == R.id.next1button) {
if (splitMethodString.equals("byWhoHadWhat")) {
byWhoHadWhat_CalculationFragment1 = new byWhoHadWhatCalculationFragment1();
splitItFragmentTransaction.replace(R.id.fragment_container, byWhoHadWhat_CalculationFragment1);
splitItFragmentTransaction.addToBackStack(null);
splitItFragmentTransaction.commit();
//Replace Next Button with Calculate & Back Button
nextButton.setVisibility(View.GONE);
calculateButton.setVisibility(View.VISIBLE);
backButton.setVisibility(View.VISIBLE);
} else if (splitMethodString.equals("evenly")) {
//Replace Fragment
evenSplit_CalculationFragment1 = new evenSplitCalculationFragment1();
splitItFragmentTransaction.replace(R.id.fragment_container, evenSplit_CalculationFragment1);
splitItFragmentTransaction.addToBackStack(null);
splitItFragmentTransaction.commit();
//Replace Next Button with Calculate & Back Button
nextButton.setVisibility(View.GONE);
calculateButton.setVisibility(View.VISIBLE);
backButton.setVisibility(View.VISIBLE);
}
}
else if (v.getId() == R.id.backButton){
splitItFragmentTransaction.replace(R.id.fragment_container, partySize_MethodFragment);
splitItFragmentTransaction.addToBackStack(null);
splitItFragmentTransaction.commit();
//Replace Next Button with Calculate & Back Button
nextButton.setVisibility(View.VISIBLE);
calculateButton.setVisibility(View.GONE);
backButton.setVisibility(View.GONE);
}
else if (v.getId() == R.id.calculateButton){
//Convert Strings pulled from Fragments into Doubles
numberInPartyDouble = Double.parseDouble(numberInPartyString);
billGrandTotalDouble = Double.parseDouble(billGrandTotalString.replace(",",""));
eachPersonOwesESString = String.valueOf(billGrandTotalDouble/numberInPartyDouble);
//Replace Even Split Fragment 1 with Even Split Fragment 2
evenSplit_CalculationFragment2 = new evenSplitCalculationFragment2();
splitItFragmentTransaction.replace(R.id.fragment_container, evenSplit_CalculationFragment2);
splitItFragmentTransaction.addToBackStack(null);
splitItFragmentTransaction.commit();
//Send data to Even Split Fragment 2
evenSplit_CalculationFragment2.tellMeWhatEachPersonOwesES(eachPersonOwesESString);
//Hide Calculate Button
calculateButton.setVisibility(View.GONE);
}
}
}
片段2:
package apps.created.jay.splititapp;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by Jay on 02/06/2015.
*/
public class evenSplitCalculationFragment2 extends Fragment {
String eachPersonOwesThisESString;
TextView amountEachPersonOwesES;
View evenSplitView2;
Communicator comm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Define the View and inflate Fragment 2
evenSplitView2 = inflater.inflate(R.layout.even_split_calculation_fragment_layout_2, container, false);
return evenSplitView2;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
amountEachPersonOwesES = (TextView) evenSplitView2.findViewById(R.id.even_split_amount_due);
comm = (Communicator) getActivity();
}
//COMMUNICATIONS
//What Each Person Owes (from Main Activity)
public void tellMeWhatEachPersonOwesES (String eachPersonOwesThisESString) {
amountEachPersonOwesES.setText(eachPersonOwesThisESString);
}
}
接口:
public interface Communicator {
public void respond (String data);
public void partyOfWhat (String data);
public void billGrandTotal (String data);
}
答案 0 :(得分:0)
您需要回调才能在片段和活动之间进行通信
这是谷歌页面的快速示例:
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
}
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
我建议您从上面的链接下载样本并进行播放。它非常好。
我希望这有助于你