Java 我想自动将数据添加到TableLayout。我写了所有的代码,但我得到了在互联网上到处搜索的错误。请帮我

Java 我想自动将数据添加到TableLayout。我写了所有的代码,但我得到了在互联网上到处搜索的错误。请帮我,java,android,android-studio,Java,Android,Android Studio,这是我的主课。我创建了TableLayout,并在TableLayout中添加了一个TableRow。我的表格行中有3个文本视图。我想在插入数据时自动添加TableRow 为此,我创建了AsyncTask以从php获取数据,并将其转换为list(模型列表),然后将其发送到我的表中 public class MainActivity extends AppCompatActivity { private TableLayout myTable; private TableRow tr; priva

这是我的主课。我创建了TableLayout,并在TableLayout中添加了一个TableRow。我的表格行中有3个文本视图。我想在插入数据时自动添加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类MainActivity扩展了AppCompatActivity{
私家桌布局myTable;
私人桌椅tr;
私有文本视图txtId、txtName、txtnamname、txtAge、txtsername、txtPassword;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTable=(TableLayout)findViewById(R.id.myTable);
字符串类型=“geldim”;
BackgroundProcess BackgroundProcess=新的BackgroundProcess(本);
backgroundProcess.execute(类型);
}
公共void setData(List getList){
对于(int i=0;i
}

这是我的背景课:

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);
}
公共类后台进程扩展异步任务{
语境;
私有MainActivity MainActivity=新的MainActivity();
背景过程(上下文ctx){
上下文=ctx;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串a=参数[0];
字符串myLink=”http://192.168.31.129/showData.php";
如果(a.equals(“geldim”)){
试一试{
系统输出打印LN(1111);
URL=新URL(myLink);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod(“GET”);
int respondCode=httpURLConnection.getResponseCode();
if(respondCode==HttpURLConnection.HTTP\u OK){
系统输出打印LN(2223);
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream,“iso-8859-1”);
字符串结果=”;
字符串行=”;
而((line=bufferedReader.readLine())!=null){
结果+=行;
}
系统输出打印LN(3333);
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
返回结果;
}否则{
返回(“未成功”);
}
}捕获(例外e){
e、 printStackTrace();
}
}否则{
返回null;
}
返回null;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的void onPostExecute(字符串值){
系统输出打印项次(值);
List getList=newarraylist();
if(值!=null){
试一试{
JSONObject对象=新的JSONObject(值);
JSONArray数组=object.getJSONArray(“menim”);
System.out.println(“saybu qederdir”+array.length());

对于(int i=0;i我想知道的是,您是如何避免内存不足错误的。您正在后台类中创建一个新的主活动,它将创建一个新的后台类,该类将创建一个新的主活动等。我的计算机有16gb ram和1 tb hdd 256 gb ssd。可能它功能强大。但我的问题不是内存错误,它是空的指针错误我在internet上到处搜索,但没有人知道,也从来没有人问过这样的问题!我建议您在修复另一个错误后将获得一个指针错误。在异步类中调试非常困难。我建议您删除线程,直到问题解决为止。您将获得更有用的logcat.Cur实际上,我们甚至不知道哪个控件是空的。也是的,他们有。非常感谢。我发现了错误。但真的不知道我可以用什么其他方法。问题是异步任务工作得很晚,您需要编写代码,其他按钮和任务必须在onCreate函数中编写。因为异步任务必须完成,然后才能工作在上面。
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)