Java 如何将数据从片段类传递到活动
请注意,我无法将Java 如何将数据从片段类传递到活动,java,android,arrays,android-studio,android-fragments,Java,Android,Arrays,Android Studio,Android Fragments,请注意,我无法将阵列列表数据发送到活动 数据是保存在ArrayList中的URL,但是在使用bundle extras之后,我得到了null指针异常。我还尝试捕获异常,但使用get extra string获得的数据仍然为null // below is the code i used to pass the data Bundle extras = new Bundle(); extras.putString("VidUrl",VideoLecturesUrl.get(po
阵列列表
数据发送到活动
数据是保存在ArrayList
中的URL,但是在使用bundle extras之后,我得到了null指针异常。我还尝试捕获异常,但使用get extra string获得的数据仍然为null
// below is the code i used to pass the data
Bundle extras = new Bundle();
extras.putString("VidUrl",VideoLecturesUrl.get(position));
extras.putString("bookUrl",bookUrl.get(position));
extras.putString("VidTitle",titleList.get(position));
Intent intent = new Intent(getActivity(),DetailsView.class);
intent.putExtras(extras);
startActivity(intent);
// below is the receiving activity
Intent intent = getIntent();
extras =intent.getExtras();
if (extras!=null){
try {
Video_Url = extras.getString("VidUrl");
BookUrl = extras.getString("bookUrl");
Title = extras.getString("VidTitle");
Toast.makeText(this,Video_Url,Toast.LENGTH_SHORT).show();
Toast.makeText(this,BookUrl,Toast.LENGTH_SHORT).show();
Toast.makeText(this,Title,Toast.LENGTH_SHORT).show();
videotexTitle.setText(Title);
setTitle(Title);
setVideo(Video_Url.toString());
}catch (Exception e){
e.printStackTrace();
}
} else {
Toast.makeText(this, "the extrass is empty", Toast.LENGTH_SHORT).show();
}
在片段中
public interface NoticeFragmentListener {
public void onFragmentSendArray(ArrayType yourArray);
}
NoticeFragmentListener listener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeFragmentListener so we can send events to the host
listener = (NoticeFragmentListener) context;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
}
}
//metod to send array
public void SendArray(ArrayType yourArray){listener.onFragmentSendArray(yourArray);}
不活跃
public class yourActivity extends AppCompatActivity implements YourFragment.NoticeFragmentListener
并接收阵列
@Override
public onFragmentSendArray(ArrayType yourArray) {
}
为了简单起见,请使用SharedReferences来实现此目的当我尝试将数据保存到变量时,您在哪里得到nullpointerexception?以便在我尝试toast数据以确认其仍然为空stringOK bro时可以使用itEven on