Java 当我使用Async时,Button onClick得到了2条错误消息
我的代码有什么问题?它显示错误为: 1。无法解析onCreate“onCreate.android.os.Bundle”方法Java 当我使用Async时,Button onClick得到了2条错误消息,java,android,android-studio,Java,Android,Android Studio,我的代码有什么问题?它显示错误为: 1。无法解析onCreate“onCreate.android.os.Bundle”方法 2.方法不会在@override上重写其超类中的方法 这是收到错误消息的部分 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.
2.方法不会在@override上重写其超类中的方法 这是收到错误消息的部分
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BUTTON = (Button) findViewById(R.id.button);
BUTTON.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DemoAsync UploadFile = new DemoAsync();
UploadFile.execute();
}
});
}
这是MainActivity.java上的全部代码
package com.example.threadexample;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public static final String DEMO_ASYNC = "DemoAsync";
private TextView B_PRESSED;
Button BUTTON;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
B_PRESSED = (TextView) findViewById(R.id.myTextView);
DemoAsync demoAsync = new DemoAsync();
demoAsync.execute("Button Pressed");
}
private class DemoAsync extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... params) {
Log.d(DEMO_ASYNC, "status : doInBackground");
try {
Thread.sleep(20000);
} catch (Exception e) {
Log.d(DEMO_ASYNC, e.getMessage());
}
return params[0];
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
B_PRESSED.setText(s);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BUTTON = (Button) findViewById(R.id.button);
BUTTON.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DemoAsync UploadFile = new DemoAsync();
UploadFile.execute();
}
});
}
}
}
package com.example.threadexample;
导入androidx.appcompat.app.appcompat活动;
导入android.util.Log;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
公共类MainActivity扩展了AppCompatActivity{
公共静态最终字符串DEMO\u ASYNC=“demosync”;
按private TextView按钮;
按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
B_PRESSED=(TextView)findViewById(R.id.myTextView);
DemoAsync DemoAsync=新建DemoAsync();
demosync.execute(“按下按钮”);
}
私有类DemoAsync扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
受保护的字符串doInBackground(字符串…参数){
Log.d(DEMO_ASYNC,“状态:doInBackground”);
试一试{
睡眠(20000);
}捕获(例外e){
d(DEMO_ASYNC,e.getMessage());
}
返回参数[0];
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
按B_.setText(s);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮=(按钮)findViewById(R.id.BUTTON);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
DemoAsync UploadFile=新建DemoAsync();
UploadFile.execute();
}
});
}
}
}
请详细说明我应该更改的部分,我是新来的。谢谢您的理解。试试这个,希望能奏效
package com.example.threadexample;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.marsad.wallpaperapp.R;
public class MainActivity extends AppCompatActivity {
public static final String DEMO_ASYNC = "DemoAsync";
private TextView B_PRESSED;
Button BUTTON;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
B_PRESSED = (TextView) findViewById(R.id.myTextView);
DemoAsync demoAsync = new DemoAsync();
demoAsync.execute("Button Pressed");
BUTTON = (Button) findViewById(R.id.button);
BUTTON.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DemoAsync UploadFile = new DemoAsync();
UploadFile.execute();
}
});
}
private class DemoAsync extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... params) {
Log.d(DEMO_ASYNC, "status : doInBackground");
try {
Thread.sleep(20000);
} catch (Exception e) {
Log.d(DEMO_ASYNC, e.getMessage());
}
return params[0];
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
B_PRESSED.setText(s);
}
}
}
包com.example.threadexample;
导入androidx.appcompat.app.appcompat活动;
导入android.util.Log;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入com.marsad.wallparapp.R;
公共类MainActivity扩展了AppCompatActivity{
公共静态最终字符串DEMO\u ASYNC=“demosync”;
按private TextView按钮;
按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
B_PRESSED=(TextView)findViewById(R.id.myTextView);
DemoAsync DemoAsync=新建DemoAsync();
demosync.execute(“按下按钮”);
按钮=(按钮)findViewById(R.id.BUTTON);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
DemoAsync UploadFile=新建DemoAsync();
UploadFile.execute();
}
});
}
私有类DemoAsync扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
受保护的字符串doInBackground(字符串…参数){
Log.d(DEMO_ASYNC,“状态:doInBackground”);
试一试{
睡眠(20000);
}捕获(例外e){
d(DEMO_ASYNC,e.getMessage());
}
返回参数[0];
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
按B_.setText(s);
}
}
}
它可以工作谢谢你,你能解释一下为什么我的代码不能工作吗?即使我在虚拟android手机4.0.3上执行了该程序,它仍然会返回错误消息。在我点击按钮和“按下按钮”几秒钟后,它突然停止字符串显示这是因为onCreate方法已经定义,但您创建了另一个,这就是为什么会发生此错误。我刚刚意识到程序工作了,但它并没有完全按照我想要的那样工作。我的意思是,即使我没有按下按钮,文本在20秒后会自动更改,我该怎么办?是的,谢谢,但它仍然自动关闭,有人知道为什么吗?删除此答案
03-30 18:59:49.026 3179-3196/com.example.threadexample W/dalvikvm: threadid=11: thread exiting with uncaught exception (group=0x9800f180)
03-30 18:59:49.026 3179-3196/com.example.threadexample E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.example.threadexample.MainActivity$DemoAsync.doInBackground(MainActivity.java:52)
at com.example.threadexample.MainActivity$DemoAsync.doInBackground(MainActivity.java:38)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
03-30 18:59:49.846 3179-3179/com.example.threadexample D/OpenGLRenderer: Flushing caches (mode 0)
03-30 18:59:50.386 3179-3179/com.example.threadexample D/EGL_emulation: eglMakeCurrent: 0x91275f8: ver 2 0
03-30 18:59:50.386 3179-3179/com.example.threadexample D/OpenGLRenderer: Flushing caches (mode 1)