将应用程序上下文分成片段,ViewPager

时间:2015-04-01 17:45:11

标签: android android-context

我正在编写一个使用viewpagerfullscreen片段的滑动视图的简单应用。在其中一个片段中,我想在显示一些数据之前检查互联网连接状态,但我无法获得应用程序上下文。我在这个问题上找到了很多主题,但没有一个解决方案适合我。我在片段中使用getApplication(),但即使我收到错误"Unreacheable statement"

有什么建议吗?

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;

public class MainActivity extends FragmentActivity {

ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //setting fragment view pager
    viewPager = (ViewPager)findViewById(R.id.pager);
    PagerAdapter pagerAdapter = new   PagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);
    }
}

activity_main.xml中

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

第一个片段

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CurrentWeatherFragment extends Fragment {

//flaga dla statusu polaczenia z internetem
Boolean isInternetPresent = false;

//klasa detektora połączenia
ConnectionDetector cd;


@Nullable
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.currentweather, container, false);
    return v;

    //tworzenie klasy detektora
    cd = new ConnectionDetector(getActivity().getApplicationContext());

    //sprawdzenie czy jest polaczenie z internetem
    isInternetPresent = cd.isConnectingToInternet();
    }
}

寻呼机适配器类

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class PagerAdapter extends FragmentPagerAdapter {

public PagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int arg0) {
    switch (arg0){
        case 0:
            return new CurrentWeatherFragment();
        case 1:
            return new ForecastFragment();
        default:
            break;
    }
    return null;
}

@Override
public int getCount() {
    return 2;
    }
}

2 个答案:

答案 0 :(得分:2)

CurrentWeatherFragment的{​​{1}}中,您onCreateView充气,然后立即将其退回。

View

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.currentweather, container, false); return v; //tworzenie klasy detektora cd = new ConnectionDetector(getActivity().getApplicationContext()); //sprawdzenie czy jest polaczenie z internetem isInternetPresent = cd.isConnectingToInternet(); } 移至方法的末尾:

return v;

答案 1 :(得分:0)

可能是生命周期问题。在onAttach(Activity)而非onCreateView()

中拨打该电话