Это класс мой MainClass. Я создал TableLayout и добавил один TableRow к своему TableLayout. В моем TableRow есть 3 TextView. Я хочу добавить TableRow автоматически при вставке данных
. Для этого я создал AsyncTask для получения данных из php и превращения их в список (список моделей), а затем хочу отправить их в мою таблицу.
public class MainActivity extends AppCompatActivity {
private TableLayout myTable;
private TableRow tr;
private TextView txtId, txtName, txtSurname, txtAge, txtUsername, txtPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTable = (TableLayout) findViewById(R.id.myTable);
String type="geldim";
BackgroundProcess backgroundProcess=new BackgroundProcess(this);
backgroundProcess.execute(type);
}
public void setData(List<Test> getList) {
for (int i = 0; i < getList.size(); i++) {
tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
txtId = new TextView(this);
txtName = new TextView(this);
txtSurname = new TextView(this);
txtAge = new TextView(this);
txtUsername = new TextView(this);
txtPassword = new TextView(this);
System.out.println(getList.get(i).getName());
txtId.setText(getList.get(i).getId());
txtName.setText(getList.get(i).getName());
txtSurname.setText(getList.get(i).getSurname());
txtAge.setText(getList.get(i).getAge());
txtUsername.setText(getList.get(i).getUsername());
txtPassword.setText(getList.get(i).getPassword());
tr.addView(txtId);
tr.addView(txtName);
tr.addView(txtSurname);
tr.addView(txtAge);
tr.addView(txtUsername);
tr.addView(txtPassword);
myTable.addView(tr);
}
}
}
И это мой фоновый класс:
public class BackgroundProcess extends AsyncTask<String,Void,String> {
Context context;
private MainActivity mainActivity=new MainActivity();
BackgroundProcess(Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String a=params[0];
String myLink="http://192.168.31.129/showData.php";
if(a.equals("geldim")){
try {
System.out.println(1111);
URL url=new URL(myLink);
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("GET");
int respondCode=httpURLConnection.getResponseCode();
if(respondCode==HttpURLConnection.HTTP_OK) {
System.out.println(2223);
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
System.out.println(3333);
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
}else {
return ("unsuccessful");
}
} catch (Exception e) {
e.printStackTrace();
}
}else {
return null;
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String value) {
System.out.println(value);
List<Test> getList=new ArrayList<>();
if(value!=null){
try {
JSONObject object=new JSONObject(value);
JSONArray array=object.getJSONArray("menim");
System.out.println("say bu qederdir"+array.length());
for(int i=0;i<array.length();i++){
JSONObject result=array.getJSONObject(i);
Test test=new Test(result.getInt("id"),result.getString("name"),result.getString("surname"),
result.getInt("age"),result.getString("username"),result.getString("password"));
getList.add(test);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(getList);
mainActivity.setData(getList);
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
Я получил эту ошибку при запуске:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.view.ViewConfiguration.get(ViewConfiguration.java:431)
at android.view.View.<init>(View.java:4535)
at android.view.View.<init>(View.java:4668)
at android.widget.TextView.<init>(TextView.java:824)
at android.widget.TextView.<init>(TextView.java:818)
at android.widget.TextView.<init>(TextView.java:814)
at android.widget.TextView.<init>(TextView.java:810)
at com.example.firstprog.TabOne.setData(TabOne.java:110)
at com.example.firstprog.BackgroundProcess.onPostExecute(BackgroundProcess.java:96)
at com.example.firstprog.BackgroundProcess.onPostExecute(BackgroundProcess.java:22)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)