Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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 初始应用程序加载时未加载ViewPager数据_Java_Android_Android Viewpager - Fatal编程技术网

Java 初始应用程序加载时未加载ViewPager数据

Java 初始应用程序加载时未加载ViewPager数据,java,android,android-viewpager,Java,Android,Android Viewpager,我正在使用android studio选项卡式活动创建一个应用程序我选择这个活动,以便当用户滑动它时,从json url加载一些数据,我创建了另一个类,在onCreateView上获取json数据(LayoutFlater充气器、ViewGroup容器方法和所有这些都可以正常工作,但当应用程序启动时以及从我调用mViewPager.setAdapter(MSActionSpagerAdapter)时的create方法的主活动开始除外)布局中未填充任何数据我想要的是,当加载app Main活动时,

我正在使用android studio选项卡式活动创建一个应用程序我选择这个活动,以便当用户滑动它时,从json url加载一些数据,我创建了另一个类,在
onCreateView上获取json数据(LayoutFlater充气器、ViewGroup容器
方法和所有这些都可以正常工作,但当应用程序启动时以及从我调用
mViewPager.setAdapter(MSActionSpagerAdapter)时的create方法的主活动开始除外)
布局中未填充任何数据我想要的是,当加载app Main活动时,我想要显示数据,而现在只有在从右向左滑动时才会显示数据

main活动

public class MainActivity extends AppCompatActivity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    static TextView factTitle;
    static TextView factDesc;
    static ImageView factImg;
    static ProgressBar loader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public static class PlaceholderFragment extends Fragment {
        private static final String ARG_SECTION_NUMBER = "section_number";
        public PlaceholderFragment() {
        }
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            factTitle = (TextView) rootView.findViewById(R.id.fact_label);
            factDesc = (TextView) rootView.findViewById(R.id.fact_description);
            factImg = (ImageView) rootView.findViewById(R.id.imageView);
            loader = (ProgressBar) rootView.findViewById(R.id.progressBar);
            fetchData process = new fetchData();
            process.execute();
            return rootView;
        }
    }
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
          Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }
        @Override
        public int getCount() {
            return 999;
        }
    }
}
获取数据类

public class fetchData extends AsyncTask<Void,Void,Void> {
    String data ="";
    String factTitle = "";
    String factDesc = "";
    String factImg ="";
    String singleParsed ="";
    String DoubleParsed ="";
    String tripleParsed ="";
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL("http://xxxxxx.com/api");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while(line != null){
                line = bufferedReader.readLine();
                data = data + line;
            }
            JSONArray JA = new JSONArray(data);
            for(int i =0 ;i <JA.length(); i++){
                JSONObject JO = (JSONObject) JA.get(i);
                singleParsed =  ""+JO.get("fact");
                DoubleParsed =  ""+JO.get("factdesc");
                tripleParsed =  ""+JO.get("img");
                factTitle = factTitle + singleParsed;
                factDesc  = factDesc + DoubleParsed;
                factImg  = factImg + tripleParsed;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        MainActivity.factImg.setVisibility(View.VISIBLE);
        MainActivity.factTitle.setText(this.factTitle);
        MainActivity.factDesc.setText(this.factDesc);
        Picasso.get().load(factImg).placeholder(R.drawable.defaultthumb).into(MainActivity.factImg);
        MainActivity.loader.setVisibility(View.INVISIBLE);
    }
}
公共类fetchData扩展异步任务{
字符串数据=”;
字符串factTitle=“”;
字符串factDesc=“”;
字符串factImg=“”;
字符串singleParsed=“”;
字符串DoubleParsed=“”;
字符串三元组=”;
@凌驾
受保护的空位背景(空位…空位){
试一试{
URL=新URL(“http://xxxxxx.com/api");
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream));
字符串行=”;
while(行!=null){
line=bufferedReader.readLine();
数据=数据+行;
}
JSONArray JA=新JSONArray(数据);

对于(int i=0;i您必须首先进行网络呼叫,一旦收到响应,请刷新Viewpager或设置适配器

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

    //Add below two lines in on create
        fetchData process = new fetchData();
        process.execute();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}
在异步任务的后期执行中,设置适配器或刷新页面

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    MainActivity.mViewPager.setAdapter(mSectionsPagerAdapter);

    MainActivity.factImg.setVisibility(View.VISIBLE);
    MainActivity.factTitle.setText(this.factTitle);
    MainActivity.factDesc.setText(this.factDesc);
    Picasso.get().load(factImg).placeholder(R.drawable.defaultthumb).into(MainActivity.factImg);
    MainA

像这样更新你的两个类可能会对你有所帮助

fetchDataclass:

public class fetchData extends AsyncTask<Void,Void,Void> {
    String data ="";
    String factTitle = "";
    String factDesc = "";
    String factImg ="";
    String singleParsed ="";
    String DoubleParsed ="";
    String tripleParsed ="";
    Activity mContext;
    TextView mfactTitle,mfactDesc;
    ImageView mfactImg;
    ProgressBar mProgressbar;

   public fetchData(Activity context,TextView mfactTitle,TextView 
   mfactDesc,ImageView mfactImg,ProgressBar mProgressbar){
   this.context=context;
   this.mfactTitle=mfactTitle; 
   this.mfactDesc=mfactDesc;
   this.mfactImg=mfactImg;
   this.mProgressbar=mProgressbar;
   }
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL("http://xxxxxx.com/api");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while(line != null){
                line = bufferedReader.readLine();
                data = data + line;
            }
            JSONArray JA = new JSONArray(data);
            for(int i =0 ;i <JA.length(); i++){
                JSONObject JO = (JSONObject) JA.get(i);
                singleParsed =  ""+JO.get("fact");
                DoubleParsed =  ""+JO.get("factdesc");
                tripleParsed =  ""+JO.get("img");
                factTitle = factTitle + singleParsed;
                factDesc  = factDesc + DoubleParsed;
                factImg  = factImg + tripleParsed;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        mfactImg.setVisibility(View.VISIBLE);
        mfactTitle.setText(this.factTitle);
        mfactDesc.setText(this.factDesc);
     Picasso.get().load(factImg).placeholder(R.drawable.defaultthumb).into(mfactImg);
       mProgressBar.setVisibility(View.INVISIBLE);
    }
}
public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            factTitle = (TextView) rootView.findViewById(R.id.fact_label);
            factDesc = (TextView) rootView.findViewById(R.id.fact_description);
            factImg = (ImageView) rootView.findViewById(R.id.imageView);
            loader = (ProgressBar) rootView.findViewById(R.id.progressBar);
            fetchData process = new fetchData(getActivity(),factTitle,factDesc,factImg,loader);
            process.execute();
            return rootView;
        }
    }

您需要先加载数据,然后再设置适配器。如何设置?请向我展示代码,以便在oncreate中调用您的asynctask,在asynctask onpost中设置您的viewpager适配器。这是不可能的,因为在另一个类中没有MSActionSpagerAdapter和mViewPager的实例哦,真的吗?:)可能会将它们作为构造函数或方法参数传递?我尝试了您的答案,但获取错误“mViewPager”在“com.ffff.android.randomfacts.mainActivity”中具有私有访问权限,并且当我更改为静态应用程序冻结时,将mViewPager的数据类型更改为public,而不是静态。现在获取此错误“非静态字段”mViewPager无法从st引用atic contex’替换为main activivyt.viewPages非常感谢终于成功了:)祝你有一个愉快的一天