Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将数据从片段类传递到活动_Java_Android_Arrays_Android Studio_Android Fragments - Fatal编程技术网

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