我有Viewpager机智2片段:CurrentWeatherFragment和ForecastFragment.我需要将字符串从一个传递到另一个,Iam使用如下所示的界面,但我一直得到NullPointerException,消息没有通过属性...
public class CurrentWeatherFragment extends Fragment {
SendMessage SM
public void onCreateView(...) {
String Message = "Hello"
SM.sendData(Message);
}
interface SendMessage
{
public void sendData(String message);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
SM = (SendMessage) activity;
} catch(ClassCastException e) {
throw new ClassCastException("Musisz zaimplementowac metode sendData");
}
}
}
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
public class MainActivity extends FragmentActivity implements CurrentWeatherFragment.SendMessage {
ViewPager viewPager;
@Override
protected void onCreate(Bundle …Run Code Online (Sandbox Code Playgroud)