Uploaded February 2013 | Updated September 2026, 2 weeks ago
A demonstration how to fix many different types of errors in an Android project.
Included:
- Using the Eclipse views: Console, Problems, LogCat
- Not closing an XML tag
- Incorrect string resource in XML
- Importing Android.R
- Java compile problems (missing }, incorrect method name)
- Invalid type cast (ImageButton to Button)
- Invalid type cast in a button call-back
- Uses project -- clean
- Shows: right-click, Android Tools -- Fix Project Properties
- Invalid resource naming
A demonstration how to fix many different types of errors in an Android project.
Included:
- Using the Eclipse views: Console, Problems, LogCat
- Not closing an XML tag
- Incorrect string resource in XML
- Importing Android.R
- Java compile problems (missing }, incorrect method name)
- Invalid type cast (ImageButton to Button)
- Invalid type cast in a button call-back
- Uses project -- clean
- Shows: right-click, Android Tools -- Fix Project Properties
- Invalid resource naming


![Computation in background w/ AsyncTask: Android Programming
How to use an AsyncTask to do calculations in the background of an Android Activity: guesses a number on a background thread while showing updates on the UI. Full code below.
// TODO: Change all [[ and ]] to less-than and greater-than (YouTube limitation)
package ca.demo.asynctaskdemo;
import java.util.Random;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
/**
* Demonstration of how to use an AsyncTask by cracking a password.
*/
public class MainActivity extends Activity {
private static final long PASSWORD = 12345;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
}
public void onClick_StartTask(View v) {
final long PUBLISH_RATE = 1000;
final long RANGE = 1000000;
// Start background task
PasswordGuesserTask crackerTask = new PasswordGuesserTask();
crackerTask.execute(RANGE, PUBLISH_RATE);
}
public void displayProgress(String message) {
TextView textView = (TextView) findViewById(R.id.txtStatus);
textView.setText(message);
}
public void displayAnswer(long answer) {
String message = I know the password: + answer;
TextView textView = (TextView) findViewById(R.id.txtFinalAnswer);
textView.setText(message);
}
/*
* Background task to crack the password.
*/
// Generics:
// 1. Long: Type of reference(s) passed to doInBackground()
// 2. String: Type of reference passed to onProgressUpdate()
// 3. Long: Type of reference returned by doInBackground()
// Value passed to onPostExecute()
// TODO: Change all [[ and ]] to less-than and greater-than (YouTube limitation)
private class PasswordGuesserTask extends AsyncTask[[Long, String, Long]] {
// Executed on main UI thread
@Override
protected void onPreExecute() {
super.onPreExecute();
MainActivity.this.setProgressBarIndeterminateVisibility(true);
}
// Run on background thread.
@Override
protected Long doInBackground(Long... arguments) {
// Extract arguments
long range = arguments[0];
long publishRate = arguments[1];
long guess = 0;
long count = 0;
Random rand = new Random();
while (guess != PASSWORD) {
guess = Math.abs(rand.nextLong()) % range;
count ++;
if (count % publishRate 0) {
publishProgress(
Guess #: + count,
Last guess: + guess);
}
}
return guess;
}
// Executed on main UI thread.
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
String message = ;
for (String str : values) {
message += str + , ;
}
displayProgress(message);
}
// Executed on main UI thread
@Override
protected void onPostExecute(Long result) {
super.onPostExecute(result);
displayAnswer(result);
MainActivity.this.setProgressBarIndeterminateVisibility(false);
}
}
} Computation in background w/ AsyncTask: Android Programming](https://i.ytimg.com/vi/t988JQHDbX0/mqdefault.jpg)
![Plain Old Java in Android Studio: Android Programming
Write and run plain old Java code (console output via main(), not for Android) in Android Studio.
[SOLUTION] Getting java.util.NoSuchElementException from a Scanner on System.in?
1. Expand the Gradle Scripts in your project view
2. Edit settings.gradle
3. Comment out (two //) the include :app line
Click Sync Now to resync your Gradle files
(This will change your project view; thats expected)
4. Re-run your program.
Note: If you use System.out.print() to print to the screen (note no linefeed) then it wont show up until after any pending System.in.nextXYZ() is finished. Plain Old Java in Android Studio: Android Programming](https://i.ytimg.com/vi/tGNOiVbY7Yc/mqdefault.jpg)






