这是我的MainClass类。我创建了TableLayout并将一个TableRow添加到我的TableLayout中。我的TableRow中有3个TextView。我想在数据插入时自动添加TableRow
为此,我创建了AsyncTask以从php获取数据并将其转到list(模型列表),然后希望将其发送到我的表中。
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)