将数据从片段传递到活动到第二个活动时出错

时间:2015-04-13 18:42:44

标签: android android-fragments android-studio nullpointerexception

我想出了如何从片段发送数据 - >活动 - >第二个片段。但是,当第二个片段尝试提取数据时,它会遇到空错误。首先发送数据的代码如下所示:

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值(或者它找不到它)。

1 个答案:

答案 0 :(得分:0)

我认为你正在通过

对片段的onCreatView()进行评分
  bundle = getArguments();

返回null bundle,这就是为什么你在你提到的那一行得到空指针异常的原因。尝试实现回调接口,在活动和片段之间来回发送数据。让我举个好例子。你应该参考这里发布的答案并制作你喜欢的代码并试试。

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