java - 如何在 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 安全吗?
有没有更好的方法来做到这一点?
解决方案
在片段中,您必须使用:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
t1 = (TextView) view.findViewById(R.id.username);
getActivity().setTitle("PROFILE");
}
推荐阅读
- javascript - 通过函数文本设置状态
- php - Laravel 雄辩的多重连接与 WITH
- python - python:我在测试代码中做错了什么?
- javascript - 正则表达式匹配第一个数字并且只允许 11
- swagger - Swashbuckle 使所有路由参数对于在 asp.net core 3.1 api swagger ui 中具有相同动词但有多个路由的端点都是强制性的
- excel - 如何将焦点设置为新可见的文本框
- excel - Excel - 打开文件而不选择工作表
- c# - 将对象(未定义)转换为具有类型的对象
- grafana - 比较现在和 2 小时前的值以及差异如何
- python - np.where() 只计算一次 np.random.choice() - pandas