Co jest nie tak z moim kodem? pokazuje się z błędem:
1. nie można rozwiązać metody onCreate „onCreate.android.os.Bundle”
2. Metoda nie zastępuje metody ze swojej nadklasy w @Override
Jest to sekcja, w której pojawił się komunikat o błędzie
@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();
}
});
}
To jest cały kod w witrynie 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();
}
});
}
}
}
Proszę podać szczegóły, którą sekcję mam zmienić, jestem nowy w tym. Dziękuję za zrozumienie.
0
Gebrina Divva Meuthia Zulma
26 marzec 2020, 16:25
2 odpowiedzi
Najlepsza odpowiedź
Spróbuj tego, miej nadzieję, że zadziała
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);
}
}
}
1
Bad Boy
26 marzec 2020, 16:14
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)
0
Gebrina Divva Meuthia Zulma
30 marzec 2020, 12:07
Podobne pytania
Powiązane pytania
Nowe pytania
java
Java to język programowania wysokiego poziomu. Użyj tego tagu, jeśli masz problemy z używaniem lub zrozumieniem samego języka. Ten tag jest rzadko używany samodzielnie i jest najczęściej używany w połączeniu z [spring], [spring-boot], [jakarta-ee], [android], [javafx], [hadoop], [gradle] i [maven].