首页 > 解决方案 > 如何在 Fragment 类中使用布局元素

问题描述

在这里,我使用 Fragment 在 Android 中制作了一个导航栏。
现在我需要使用 Fragment 类及其 XML 文件打印我的数据,但我无法以正常方式获取 id。

配置文件.java

        package com.example.dhruv.dez;

    import android.app.Activity;
    import android.app.Fragment;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    public class Profile extends Fragment {
        @Nullable

   //Below Line Gives Error
   BackgroundTask b = new BackgroundTask(this);

    TextView t1;

        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.profile, container, false);

        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            //Below Line Gives Error
            t1=FindViewById(R.id.username);

            getActivity().setTitle("PROFILE");


        }

    }

配置文件.xml

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content"
        android:text="PROFILE"
        android:id="@+id/username"/>

    </RelativeLayout>

背景任务.java

        public class BackgroundTask extends AsyncTask<String,Void,String>{

        Context ctx;

        public BackgroundTask(Context ctx)
        {
            this.ctx=ctx;
        }

        @Override
        protected String doInBackground(String... params) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            String work = params[0];

            if (work.equals("register")) {

                String name= params[1];
                String email = params[2];
                String mobno= params[3];
                String pass = params[4];
                try {
                    String login_url = "http://myweb.in/dhruv/signup.php";
                    URL url = new URL(login_url);
                    HttpURLConnection huc = (HttpURLConnection) url.openConnection();
                    huc.setRequestMethod("POST");
                    huc.setDoOutput(true);
                    OutputStream os = huc.getOutputStream();
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                    String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&"

                            + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") +"&"
                            + URLEncoder.encode("mobno", "UTF-8") + "=" + URLEncoder.encode(mobno, "UTF-8") +"&"
                            + URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8");
                    Log.w("Error", data);
                    bw.write(data);
                    bw.flush();
                    bw.close();
                    os.close();
                    InputStream is = huc.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"));
                    String respone = br.readLine();
                    Log.w("Response", respone);
                    is.close();
                    return respone;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

为什么我不能在我的片段类中使用后台任务对象?我可以知道为什么会出现这个问题吗?
在 Android 项目中使用 Fragmnet 安全吗?
有没有更好的方法来做到这一点?

标签: javaandroidandroid-layoutfragment

解决方案


在片段中,您必须使用:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    t1 = (TextView) view.findViewById(R.id.username);
    getActivity().setTitle("PROFILE");
}

推荐阅读