NullPointerException when using Handler for updating progressBar [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
public class MainActivity extends Activity {
Button button = (Button)findViewById(R.id.btn);
ProgressBar bar = (ProgressBar)findViewById(R.id.pbar);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
updateHandler.post(updateThread);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Handler updateHandler = new Handler(){
public void handleMessage(Message msg) {
bar.setProgress(msg.arg1);
updateHandler.post(updateThread);
};
};
Runnable updateThread = new Runnable() {
int i = 0;
#Override
public void run() {
// TODO Auto-generated method stub
System.out.println("------------");
i = i+10;
Message msg = updateHandler.obtainMessage();
msg.arg1 = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
updateHandler.sendMessage(msg);
if (i==100) {
updateHandler.removeCallbacks(updateThread);
}
}
};
}
Logcat:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.zxy.handlerpbtest/com.zxy.handlerpbtest.MainActivity}: java.lang.NullPointerException

You can rewrite this code of lines:
ProgressBar bar = (ProgressBar)findViewById(R.id.pbar);
Button button = (Button)findViewById(R.id.btn);
As:
public class MainActivity extends Activity {
Button button ;
ProgressBar bar ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar)findViewById(R.id.pbar);
button = (Button)findViewById(R.id.btn);
.....
}}
Then you will not get null pointer exception.

This line
ProgressBar bar = (ProgressBar)findViewById(R.id.pbar);
Button button = (Button)findViewById(R.id.btn);
should be in your onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ProgressBar bar = (ProgressBar)findViewById(R.id.pbar);
Button button = (Button)findViewById(R.id.btn);
}

Here is Complete Solution :
public class MainActivity extends Activity {
Button button;
ProgressBar bar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button = (Button) findViewById(R.id.btn);
bar = (ProgressBar) findViewById(R.id.pbar);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
updateHandler.post(updateThread);
}
});
}
Handler updateHandler = new Handler() {
public void handleMessage(Message msg) {
bar.setProgress(msg.arg1);
updateHandler.post(updateThread);
}
};
Runnable updateThread = new Runnable() {
int i = 0;
#Override
public void run() {
// TODO Auto-generated method stub
System.out.println("------------");
i = i + 10;
Message msg = updateHandler.obtainMessage();
msg.arg1 = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
updateHandler.sendMessage(msg);
if (i == 100) {
updateHandler.removeCallbacks(updateThread);
}
}
};
}

Related

App crashing when saving to shared preferences

I am trying to save to shared preferences. I want to be able to load from shared preferences but when I save and or load at the moment my app crashes.
I also want to be able to have my handler/runnable resume when my app starts up. How can I do this?
Here is my code:
public class MainActivity extends ActionBarActivity {
public void save(){
Editor editor = pref.edit();
editor.putInt("countertest", counter);
editor.commit();
}//end of save
public void read(){
counter = pref.getInt("countertest", counter);
}//end of read
Handler testHandler = new Handler();
int counter;
TextView testView;
Button testButton;
Runnable Runnable0;
SharedPreferences pref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testView = (TextView) findViewById(R.id.testView);
testButton = (Button) this.findViewById(R.id.testButton);
// read();
testView.setText(Integer.toString(counter));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onResume() {
//read();
final Runnable Runnable0 = new Runnable() {
#Override
public void run() {
counter += 1;
testView.setText(Integer.toString(counter));
testHandler.postDelayed(this, 1000);
// save();
}// end of if
};
/* button click */
testButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Start runnable */
testButton.setEnabled(false);
counter -= 5;
//save();
testView.setText(Integer.toString(counter));
testHandler.post(Runnable0);
}// end of onClick
});// end of blingButton onClickListener
super.onResume();
}//end of onResume
#Override
protected void onPause() {
//save();
testHandler.removeCallbacks(Runnable0);
super.onPause();
}//end of onPause
}
You haven't initialize your pref object. You have just declared it. So you need to do it on onCreate() by
pref = getSharedPreferences("MySP", 0);
OR
pref = PreferenceManager.getDefaultSharedPreferences(this);

Progress dialog does not open on preExecute in Asynctask non-activity class

I’m using ksoap to call a web service.
When Application starts or when i scroll to end of page Application fetches data from Web Service correctly and it shows data as well as i want, But When I want to show Progress Dialog in a AsyncTask it doesn’t work correctly. It shows Progress Dialog after PostExecute in a blik of eyes. So, What's wrong in my code ? I can't understand where is my problem. ;-)
I have a web service class called ElvatraWebService:
Here is a Method witch is fetch latest post with Asynctasks
private String GetLastPublicPosts(int counter){
...
...
...
...
return resTxt; //Returns JSON String
}
public String getLatestPost(int counter, Activity a){
CallLatestPost task = new CallLatestPost(counter,a);
try {
strJSON = task.execute("getLatest").get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Log.i("sss", "--" + strJSON);
return strJSON;
}
private class CallLatestPost extends AsyncTask<String, Void, String> {
private int Counter = 0;
private ProgressDialog dialog;
private Activity activity;
private Context context;
public CallLatestPost (int c,Activity actt) {
super();
this.Counter = c;
activity = actt;
context = actt;
dialog = new ProgressDialog(this.context);
}
#Override
protected String doInBackground(String... params) {
ElvatraWebService elws = new ElvatraWebService();
String tmp = elws.GetLastPublicPosts(this.Counter);
//Log.i("strJSON",strJSON);
return tmp;
}
#Override
protected void onPostExecute(String result) {
//Set response
super.onPostExecute(result);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(context);
dialog.setMessage("Connecting...");
//dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
Log.i("###Async", "=== " + "PreExec");
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
I have a Class called post. It will call getLatestPost from Elvatrawebservice.
Then it will convert JSON String to JsonArray with PostAdapter class. PostAdapter is a class extends BaseAdapter.
In Main Activity I call a local Method called LoadContent in onCreate method.
Here is MainActivity Class
public class MainActivity extends Activity {
String displayText="";
public TextView tv;
public ListView lv;
public ProgressBar pg;
int theCounter=20;
String[] strTAG = new String[] {"title","photo","date","desc","linkurl"};
//int[] intField = new int[] {R.id.title,R.id.imgPost, R.id.Date, R.id.desc, R.id.link};
ListAdapter sa;
List<HashMap<String,Object>> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recentposts);
lv = (ListView) findViewById(R.id.list);
lv.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
final int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount) {
//load more data
// Load content of listview
LoadContent();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
public void LoadContent(){
lv = (ListView) findViewById(R.id.list);
Post p = new Post(theCounter);
theCounter+=20;
if (p.GetLatestPosts(lv,MainActivity.this))
{
//tv.setText("true");
}
}
}
You have
task.execute("getLatest").get();
You should not call get() as this blocks the ui thread waiting for the result making it asynctask asynchronous no more. Just use
task.execute("getLatest");
You can return result in doInBackground and update ui in onPostExecute.

save .class name in a listview through sharedpreferences [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
hi i am making a dictionary app and i want that when i open a word from a listview , then in the word page (word.class) (where the word meaning is explained), i want to add that word into a favorite.class activity in a listview shape and later on i can retrive that word from the listview.
i want to hit the favorite button in one java class and to save that word(string which is class name for that word) in anathor activity of favorite.class. the favorite button is actually is the menu item visible on the action bar. please explain me all the code and the way how can i do this..please give some code so to acomplish it.
public class Atherosclerosis extends Activity {
// declare variables for the table of content and paragraph heading here//
ScrollView scrollView;
TextView sign_atherosclerosis;
TextView sign_id;
TextView def_id;
TextView def_atherosclerosis;
TextView riskfac_id;
TextView riskfac_atherosclerosis;
TextView pathophy_id;
TextView pathophy_atherosclerosis;
TextView Dx_id;
TextView Dx_atherosclerosis;
TextView Rx_id;
TextView Rx_atherosclerosis;
TextView prevent_id;
TextView prevent_atherosclerosis;
TextView compl_id;
TextView compl_atherosclerosis;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.atherosclerosis);
//here relate the variable for clickonlistener activity and direct scrolldown activity //
final ScrollView scrollView=(ScrollView)findViewById(R.id.scrollatherosclerosis);
TextView sign_id=(TextView)findViewById(R.id.Signandsymptomps_id);
final TextView sign_atherosclerosis=(TextView)findViewById(R.id.Signandsymptoms_atherosclerosis);
TextView def_id=(TextView)findViewById(R.id.definition_id);
final TextView def_atherosclerosis=(TextView)findViewById(R.id.definition_atherosclerosis);
TextView riskfac_id=(TextView)findViewById(R.id.riskfactor_id);
final TextView riskfac_atherosclerosis=(TextView)findViewById(R.id.riskfactor_atherosclerosis);
TextView pathophy_id=(TextView)findViewById(R.id.pathophysiology_id);
final TextView pathophy_atherosclerosis=(TextView)findViewById(R.id.pathophysiology_atherosclerosis);
TextView Dx_id=(TextView)findViewById(R.id.Diagnosis_id);
final TextView Dx_atherosclerosis=(TextView)findViewById(R.id.diagnosis_atherosclerosis);
TextView Rx_id=(TextView)findViewById(R.id.treatment_id);
final TextView Rx_atherosclerosis=(TextView)findViewById(R.id.treatment_atherosclerosis);
TextView prevent_id=(TextView)findViewById(R.id.prevention_id);
final TextView prevent_atherosclerosis=(TextView)findViewById(R.id.prevention_atherosclerosis);
TextView compl_id=(TextView)findViewById(R.id.complication_id);
final TextView compl_atherosclerosis=(TextView)findViewById(R.id.complication_atherosclerosis);
// this code is used for the action bar color change//
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// this is the code for jumping from the table of content to the paragraph//
sign_id.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(300, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (sign_atherosclerosis.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
}
});
def_id.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(300, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (def_atherosclerosis.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
}
});
riskfac_id.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(300, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (riskfac_atherosclerosis.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
}
});
pathophy_id.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(300, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (pathophy_atherosclerosis.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
}
});
Dx_id.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(300, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (Dx_atherosclerosis.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
}
});
Rx_id.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(300, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (Rx_atherosclerosis.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
}
});
prevent_id.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(300, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (prevent_atherosclerosis.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
}
});
compl_id.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
scrollView.post(
new Runnable() {
#Override
public void run() {
new CountDownTimer(300, 20) {
#Override
public void onTick(long millisUntilFinished) {
scrollView.scrollTo(0, (int) (compl_atherosclerosis.getBottom()-millisUntilFinished));
}
#Override
public void onFinish() {
}
}.start();
}
}
);
}
});
}
// this is for the options selected from the menu button of mobile//
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.atherosclerosis, menu);
return true;
}
// for starting activity from the option or menu//
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.id_search:
Intent newActivity0 = new Intent(this,Search.class);
startActivity(newActivity0);
return true;
case R.id.id_favorit:
SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("favorite", "com.kmcpesh.shortreviewofcardiology.Favorite");
editor.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
this is my favorite.class activity
public class Favorite extends Activity {
private TextView mEmptyText;
private LinearLayout mBookmarkLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite);
mEmptyText = (TextView) findViewById(R.id.empty_textview);
mBookmarkLayout = (LinearLayout) findViewById(R.id.bookmark_insert_point);
getAllKeys();
}
private void getAllKeys()
{
SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
Map<String,?> keys = sp.getAll();
int count = 0;
for(Map.Entry<String,?> entry : keys.entrySet())
{
String value = entry.getValue().toString();
System.out.println("!!!!!!!!!!!!!!!!!value = "+value);
String delimiter = ",";
String[] values_array = value.split(delimiter);
addBookmark(values_array);
count++; //keep track of the number of bookmarks
}
//if there are no bookmarks, display a text view saying so. Otherwise, make the text view go away
if (count == 0)
{
mEmptyText.setVisibility(View.VISIBLE);
mEmptyText.setText(getString(R.string.no_bookmark));
}
else
mEmptyText.setVisibility(View.GONE);
}
#SuppressWarnings("deprecation")
private void addBookmark(String[] values_array)
{
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.favorite, null);
TextView text = (TextView) v.findViewById(R.id.bookmark_text);
text.setText(values_array[1]);
// insert into main view
mBookmarkLayout.addView(v, 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
System.out.println("!!!!!!!!!!!!!!!!!!!!Just added a view");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.favorite, menu);
return true;
}
}
SharedPreferences in Android is generally used to store key=value pairs, not lists such as the one you describe. To store a value into SharedPreferences, you can do this:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor ed = sp.edit();
ed.putString("myKey", "myValue");
And then to retrieve a string key from SharedPreferences, you would do this:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String value = sp.getString("myKey", "");
You could have a comma-delimited list of favorite words stored in here, and just parse the list:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor ed = sp.edit();
ed.putString("favorites", "dog,cat,bird,fish");
And then to retrieve them:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String favorites = sp.getString("myKey", "");
String[] words = values.split(",");
System.out.println(Arrays.toString(words));
Then you have an array containing all of your favorite words.
[dog, cat, bird, fish]

error in java android programming

I'm trying to make a property animation on ImageButton, a button will start the animation when clicked on, but in the private static class ImageButtonAnimatorHelper method, it's showing an error.
public class MainActivity extends Activity {
// ImageButton jackfruit = (ImageButton) findViewById(R.id.btnjackfruit);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but = (Button) findViewById(R.id.btn);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ball();
}
public void ball() {
ImageButton ball = (ImageButton) findViewById(R.id.btnball);
ball.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
// ImageButton b = (ImageButton) findViewById(R.id.btnball);
ImageView banan = (ImageView) findViewById(R.id.banana);
banan.setVisibility(View.VISIBLE);
} // onClick
}); // setOnClickListener
ObjectAnimator horizontalAnimator = ObjectAnimator.ofInt(
new ImageButtonAnimatorHelper(ball), "marginLeft", 0, 600);
horizontalAnimator.setDuration(2000);
horizontalAnimator.setRepeatCount(ValueAnimator.INFINITE);
horizontalAnimator.setRepeatMode(ValueAnimator.REVERSE);
horizontalAnimator.setInterpolator(new LinearInterpolator());
horizontalAnimator.start();
} // onCreate
private static class ImageButtonAnimatorHelper {
ImageButton ballButton;
public ImageButtonAnimatorHelper(ImageButton imagebutton) {
ballButton = imagebutton;
}
public void setMarginLeft(int margin) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) ballButton
.getLayoutParams();
params.leftMargin = margin;
ballButton.setLayoutParams(params);
} // setMarginLeft
} // ImageButtonAnimatorHelper
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I'm confused with when to use private/public/abstract class..

Why isn't my Handler working?

When I go to run the app everything seems fine but when I press the Start Button it doesnt display Hello I even tried to set text before the thread and it still didnt work. Why would this be happening?
Here is the code:
public class MainActivity extends Activity implements OnClickListener {
Handler mHandler;
Button enter;
Button start;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enter = (Button)findViewById(R.id.enter);
start = (Button)findViewById(R.id.start);
display =(TextView)findViewById(R.id.Display);
mHandler = new Handler(){
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle bundle = msg.getData();
String string = bundle.getString("myKey");
display.setText(string);
}
};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.enter:
break;
case R.id.start:
Thread setText = new Thread(){
#Override
public void run() {
// TODO Auto-generated method stub
super.run();
Message msg= Message.obtain();
Bundle bundle = new Bundle();
String dateString;
dateString = "Hello";
bundle.putString("myKey", dateString);
msg.setData(bundle);
mHandler.sendMessage(msg);
}
};
setText.start();
break;
}
}
}
You didn't register Listeners on your enter/start button in the onCreate() method. Just call f.e.:
enter.setOnClickListener(this);

Categories

Resources