简体   繁体   English

如何在片段中使用 findviewById 以前是一项活动

[英]how to use findviewById in a fragment was previously an activity

how to put this class in a fragment.如何将这个类放在一个片段中。

The problem cannot resolve method findviewbyID问题无法解决方法 findviewbyID

could you please help me with a solution i need an alternative to make this code work你能帮我解决一个解决方案吗,我需要一个替代方案来使这段代码工作

it was a activity i am trying to convert it into fragment这是我试图将其转换为片段的活动

public class MainActivity extends Fragment {

    TextView fajrTV;
    TextView zuhrTV;
    TextView asrTV;
    TextView maghribTV;
    TextView ishaTV;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_parse_salah_times, container, false);
        new GetMethodEx().execute("http://muslimsalat.com/11717/daily/4.json?key=ea4417ebf152c478082e1ff6a1a759d9");
        return rootView;}

    public class GetMethodEx  extends AsyncTask<String, Void, InputStream> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            fajrTV = (TextView) findViewById(R.id.fajr);
            zuhrTV = (TextView) findViewById(R.id.zuhr);
            asrTV = (TextView) findViewById(R.id.asr);
            maghribTV = (TextView) findViewById(R.id.maghrib);
            ishaTV = (TextView) findViewById(R.id.isha);
        }

        @Override
        protected InputStream doInBackground(String... params) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(params[0]);
                HttpResponse response = client.execute(post);
                int status = response.getStatusLine().getStatusCode();
                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream in = entity.getContent();
                    return in;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(InputStream in) {
            super.onPostExecute(in);
            try {
                JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

                String fajr = null;
                String zuhr = null;
                String asr = null;
                String maghrib = null;
                String isha = null;

                reader.beginObject();
                while (reader.hasNext()) {
                    String name = reader.nextName();
                    if (name.equals("items")) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            reader.beginObject();
                            while (reader.hasNext()) {
                                String innerName = reader.nextName();
                                final boolean isInnerNull = reader.peek() == JsonToken.NULL;
                                if (innerName.equals("fajr") && !isInnerNull) {
                                    fajr = reader.nextString();
                                } else if (innerName.equals("dhuhr") && !isInnerNull) {
                                    zuhr = reader.nextString();
                                } else if (innerName.equals("asr") && !isInnerNull) {
                                    asr = reader.nextString();
                                } else if (innerName.equals("maghrib") && !isInnerNull) {
                                    maghrib = reader.nextString();
                                } else if (innerName.equals("isha") && !isInnerNull) {
                                    isha = reader.nextString();
                                } else {
                                    reader.skipValue();
                                }
                            }
                            reader.endObject();
                        }
                        reader.endArray();
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
                reader.close();

                fajrTV.setText(fajr);
                zuhrTV.setText(zuhr);
                asrTV.setText(asr);
                maghribTV.setText(maghrib);
                ishaTV.setText(isha);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Try this尝试这个

public class YourFragment extends Fragment{
   View rootView; // create your rootView in here
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
      ... 
      rootView = inflater.inflate(R.layout.your_fragment, container, false);
      return rootView;
   }

   ....

   public class GetMethodEx  extends AsyncTask<String, Void, InputStream> {
        ...
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            fajrTV = (TextView) rootView.findViewById(R.id.fajr); // use the rootView
            zuhrTV = (TextView) rootView.findViewById(R.id.zuhr);
            asrTV = (TextView)  rootView.findViewById(R.id.asr);
            maghribTV = (TextView) rootView.findViewById(R.id.maghrib);
            ishaTV = (TextView) rootView.findViewById(R.id.isha);
        }
    }
    ...
}

Hope this help希望这有帮助

Fragment s do not include such a method. Fragment不包括这种方法。 If you want to reproduce a similar functionality, keep a reference to the View you return in onCreateView , then call findViewById on that reference.如果您想重现类似的功能,请保留对您在onCreateView返回的View的引用,然后在该引用上调用findViewById

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果类A扩展了类B扩展了Activity,如何正确使用findViewById - How to correctly use findViewById if a class A extends class B extends Activity 我想在底部导航活动的片段中添加秒表,但 findviewbyid 显示错误。 如何解决? - I wanted to add Stopwatch in a fragment with bottom navigation activity but findviewbyid shows error. How to resolve? 如何在片段中使用基础活动? - How to use base activity in fragment? 如何在Activity外访问findViewById() - How access findViewById() outside Activity Android:如何从片段中以编程方式单击先前位于活动堆栈中活动中的按钮? - Android: How to programmatically click on a button previously located in an activity in the activity stack, from a fragment? 来自活动的片段项的空指针异常findViewById - Null Pointer Exception findViewById of fragment item From Activity 父活动片段中的EditText的NullPointerException findViewByID - NullPointerException findViewByID of a EditText inside a fragment from his parent activity 如何在Android父片段中查找ViewById()(获取ClassCastException)? - How to findViewById() in Android parent fragment (getting a ClassCastException)? 片段中的findViewById,如何在onCreateView之外获取视图 - findViewById in fragment , how to get view outside onCreateView 标签活动中如何在片段内使用滚动视图? - how to use scrollview inside a fragment in tab activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM