Здесь я сделал панель навигации в Android, используя фрагмент.
Теперь мне нужно распечатать свои данные, используя класс Fragment и его XML-файл, но я не могу получить идентификатор обычным способом.
Profile.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");
}
}
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>
BackgroundTask.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();
}
}
Почему я не могу использовать объект фоновой задачи в своем классе фрагментов?
Могу ли я узнать, почему существует эта проблема?
И безопасно ли использовать fragmnets в проектах Android?
Есть ли лучшие способы сделать это?