Updating a TextView out an AsyncTask - java

I am trying the apply this example to my project but I am getting the following error:
Error:(26, 33) error: cannot find symbol method findViewById(int)
This is how my code looks like:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView display = (TextView) findViewById(R.id.textView1);
display.setText("Hello Android!");
Connect myConnect = new Connect();
myConnect.execute();
}
}
class Connect extends AsyncTask<Void, Void, Integer> {
TextView Text1 = (TextView) findViewById(R.id.textView1);
#Override
protected void onPreExecute(){
}
#Override
protected Integer doInBackground(Void... params) {
return 0;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onPostExecute(0);
Text1.setText("Text Changed");
}
}
What am I missing here?
How can I reference the TextView through the AsyncTask?

This is incorrect :
class Connect extends AsyncTask<Void, Void, Integer> {
TextView Text1 = (TextView) findViewById(R.id.textView1);
findViewById is a method of Activity class. not AsyncTask
to fix it, use this:
Connect myConnect = new Connect(display);
and create constructor:
class Connect extends AsyncTask<Void, Void, Integer> {
TextView Text1;
public Connect(TextView tv) {
Text1 = tv;
}
and you have got the other error:
#Override
protected void onProgressUpdate(Void... values) {
super.onPostExecute(0);
Text1.setText("Text Changed");
}
change it into this:
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Text1.setText("Text Changed");
}
SUMMARISE
//one file
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView display = (TextView) findViewById(R.id.textView1);
display.setText("Hello Android!");
Connect myConnect = new Connect(display);
myConnect.execute();
}
}
//possible the other file. but you can put the class below as nested - just move previous paranthesis to the end of the code
class Connect extends AsyncTask {
TextView Text1;
public Connect(TextView tv) {
Text1 = tv;
}
#Override
protected void onPreExecute(){
}
#Override
protected Integer doInBackground(Void... params) {
/*publish your progress here
publishProgress(...);
*/
return 0;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
Text1.setText("Text Changed");
}
}

Your text view is related to your MainActivity so create the global reference in activity class only and use it for sub class also.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView Text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Text1 = (TextView) findViewById(R.id.textView1);
Text1.setText("Hello Android!");
Connect myConnect = new Connect();
myConnect.execute();
}
class Connect extends AsyncTask<Void, Void, Integer> {
#Override
protected void onPreExecute(){
}
#Override
protected Integer doInBackground(Void... params) {
return 0;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onPostExecute(0);
Text1.setText("Text Changed");
}
}
}

Use your view inside onCreate(). onProgressUpdate() will be called on the main application thread (a.k.a., UI thread). As said by CommansWare
Global
private TextView Text1;
use as:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView display = (TextView) findViewById(R.id.textView1);
Text1 = (TextView) findViewById(R.id.textView1); //here
.........
Set as it is
#Override
protected void onProgressUpdate(Void... values) {
super.onPostExecute(0);
Text1.setText("Text Changed");
}

findViewById() is not accessible inside AsyncTask, the method belongs to activity, so you cannot initialize view using id inside Asynctask. Initialize your view in activity inside onCreate and use the instance in async task
public class MainActivity extends AppCompatActivity {
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.textView1);
display.setText("Hello Android!");
Connect myConnect = new Connect();
myConnect.execute();
}
class Connect extends AsyncTask<Void, Void, Integer> {
#Override
protected void onPreExecute(){
}
#Override
protected Integer doInBackground(Void... params) {
return 0;
}
#Override
protected void onProgressUpdate(Void... values) {
}
protected void onPostExecute(Long result) {
display.setText("Text Changed");
}
}}

Related

Android : How can I use method emitBubbles() in Question1 class

I'm doing a quiz and I don't want to write in all of new Activities this method but I can't make it work. I was trying to make another java class to put there all methods but how do that
Hope u gonna help me guys
//Main class
public class MainActivity extends AppCompatActivity {
private Button button;
BubbleEmitterView bubbleEmitter;
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emitBubbles();
bubbleEmitter = findViewById(R.id.bubbleEmitter);
button = findViewById(R.id.startButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startQuiz();
}
});
}
public void emitBubbles()
{
handler.postDelayed(new Runnable() {
#Override
public void run() {
//determine the size of the bubbles and the range
int size = new Random().nextInt(81)+20;
bubbleEmitter.emitBubble(size);
emitBubbles();
}
},
//determine the range for the bubbles to appear
new Random().nextInt(301)+100);
}
//question class
public class Question1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
}
}

Updating the status in UI methods from a non-UI thread

I have an Activity that includes a TextView to display the current status of the app. Besides MainActivity there is another class that is used for checking for updates and then updating the app if appropriate.
This is the important part of the code.
public class MainActivity extends AppCompatActivity {
private TextView status;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
status = findViewById(R.id.status);
new Update().check();
}
public void setStatus(String status) {
this.status.setText(status);
}
}
How can I update the status TextView from within the Update class?
public class Update extends Activity {
public void check(final Context c) {
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView status = findViewById(R.id.status);
status.setText("Checking for updates...");
}
});
// final TextView status = getApplicationContext().findViewById(R.id.status);
// status.setText("Checking for updates");
// Check for updates...
}
public class UpdateApp extends AsyncTask<String,Void,Void> {
private Context context;
public void setContext(Context contextf){
context = contextf;
}
#Override
protected Void doInBackground(String... arg0) {
// Update the app
}
}
}
Attempting to run getApplicationContext().findViewById(R.id.status) crashes the app. When I put it in a try block it still crashes the app - how can that be when I catch a Throwable object? ... that doesn't make sense!!!
Using runOnUiThread the app runs but the status is not updated.
Put the UpdateApp AsynTask in new file.
public class MainActivity extends AppCompatActivity {
private TextView status;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
status = findViewById(R.id.status);
new UpdateApp(status).execute();
}
}
UpdateApp.java
public class UpdateApp extends AsyncTask<String,Void,Void> {
private TextView status;
public UpdateApp(TextView status){
this.status = status;
}
#Override
protected void onPostExecute(String result) {
status.setText("Update complete");
}
#Override
protected void onPreExecute() {
status.setText("Checking for updates...");
}
#Override
protected Void doInBackground(String... arg0) {
// Update the app
}
}
You just don't need another class to make it works, just copy your UpdateApp class inside MainActivity to access your status view.
In case you want your UpdateApp class to be in a different file (witch I recommend, to separate classes responsibility) you'd need a callback: a reference to your activity to update the status view from the AsyncTask class. The way you can make it is using Interfaces. Take a look to this example
MainActivity.java
public class MainActivity extends AppCompatActivity implements CallbackInterface {
private TextView status;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
status = findViewById(R.id.status);
new LoaderClass(this).execute();
}
#Override
public void setStatusText(String text) {
status.setText(text);
}
}
CallbackInterface.java
public interface CallbackInterface {
void setStatusText(String text);
}
LoaderClass.java
public class LoaderClass extends AsyncTask<String, Void, Void> {
private CallbackInterface callbackInterface;
public LoaderClass(CallbackInterface callback) {
callbackInterface = callback;
}
#Override
protected Void doInBackground(String... arg0) {
callbackInterface.setStatusText("Loaded text");
return null;
}
}
If you are in any Activity you can use runonUIthread() like -
runOnUiThread(new Runnable() {
#Override
public void run () {
// do your magic here
}
});

In AsyncTask on onPostExecute the textview value is not setting

Textview value is crashing with null pointer exception
Below is the code which i am using. Is there any issue in the code.
public class MainActivity extends Activity {
Button btn;
int i =0;
SharedPreferences.Editor preferences;
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
txt = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new LongOperation().execute("");
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
txt.setText("Executed ----------------------- ");
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
}
After it is coming to onPostExecute the textview value is not setting the value.
Please help me. Thanks in advance.
Textview value is not setting because the value of id may be null. check it.

Return a Malayam String from another class in android/java

May be a very silly but i am not able to figured it out.
requirement is to print Malayalam text..
I am able to achieve in following way.
class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
JSONObject j = new JSONObject();
TextView tv=(TextView) findViewById(R.id.textview);
j.put("contain", "മലയാളം");
tv.setText(j.getString("contain"));
}
}
the above is working great..but if I am trying as below the text view prints only question marks like --> "?????"
public class Language_Mapping{
String appName = "മലയാളം";
public String get_Data(){
return appName;
}
}
class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
Language_Mapping obj = new Language_Mapping();
TextView tv=(TextView) findViewById(R.id.textview);
tv.setText(obj.get_Data());
}
}
Why??

How To Extract Table Data Via Android

I'm attempting to extract data from a table on a web page. Thus far I've been able to extract the data from the title tag - but not the table data. How can this be accomplished using the source shown below?
SOURCE:
public class MainActivity extends Activity {
TextView tv;
final String URL="http://example.com";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
#Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
#Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}
EDIT - In Response to Matej Spili's comment below:
public class MainActivity extends Activity {
TextView tv;
final String URL="http://example.com";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
#Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
#Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Element tableHeader = doc.select("tr").first();
for( Element element : tableHeader.children() )
{
// Here you can do something with each element
System.out.println(element.text());
tv.setText(element.text());
}
title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}
^- (causes error: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views)
Pass the view that you want to update as constructor argument for the MyTask like
new MyTask(tv).execute(URL);
Change your MyTask accordingly to accept this argument.

Categories