gpt4 book ai didi

android - 无法在我的异步类中找到方法

转载 作者:行者123 更新时间:2023-11-30 01:29:27 26 4
gpt4 key购买 nike

我正在创建一个可以使用 HTTP DELETE 从 JSON 中删除数据的 Android 应用程序。但我遇到了问题。我创建了 2 个不同的文件。 MainActivity 之一和另一个 Async。

主 Activity .java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
onClickButtonListner();
}

public void onClickButtonListner() {
final Button btn = (Button) findViewById(R.id.delete_btn);
final String URL = myurl;
btnDelete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Async asyn = new Async(URL);
asyn.execute();
}
});
}
}

异步.java

public class Async extends AsyncTask<Void, Void, String>  {
String murl;
public Async(String url) {
murl = url;
}

@Override
protected String doInBackground(Void... params) {
HttpURLConnection httpURLConnection;
URL url ;
try {
final EditText id= (EditText)findViewById(R.id.delete_id);
**showing unable to resolve method findViewById**
String delete_url = murl + "/" + id.getText().toString();
url = new URL(delete_url);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestMethod("DELETE");
httpURLConnection.connect();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}

所以我的问题是我的 Async 类显示无法找到 findViewById 方法。

所以请帮我解决这个问题谢谢

最佳答案

尝试如下更改代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
onClickButtonListner();
}

public void onClickButtonListner() {
final Button btn = (Button) findViewById(R.id.delete_btn);
final String URL = myurl;
final EditText id= (EditText)findViewById(R.id.delete_id);
btnDelete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Async asyn = new Async(URL,id.getText().toString());
asyn.execute();
}
});
}
}

Async.java

    public class Async extends AsyncTask<Void, Void, String>  {
String murl,id;
public Async(String url,String _id) {
murl = url;
id=_id;
}

@Override
protected String doInBackground(Void... params) {
HttpURLConnection httpURLConnection;
URL url ;
try {
String delete_url = murl + "/" + id;
url = new URL(delete_url);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestMethod("DELETE");
httpURLConnection.connect();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}

关于android - 无法在我的异步类中找到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35971451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com