My purpose is simple,I want to creat a countdown that count from 10 to 1.I have tried using countdown given by google but I can't make it as a thread,so I use this way to creat the same function but I had a problem with this code.My app getting crashed when I use this threads code.Please help me man.
public class MainActivity extends Activity {
TextView textView;
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
String string = textView.getText().toString();
int num = Integer.parseInt(string);
num = num-1;
string = Integer.toString(num);
textView.setText(string);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Thread myThread = new Thread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
for(int i = 10; i>0;i--){
try {
Thread.sleep(1000);
//handler.sendMessage(handler.obtainMessage());
handler.sendMessage(handler.obtainMessage());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
myThread.start();
}
}
Your problem isnt with the thread, its with these lines
String string = textView.getText().toString();
int num = Integer.parseInt(string);
You probably have that textView starting out with some text in the XML("Large Text"). Remove it. "Large Text" isn't a number, so when you call parseInt() on that original string its trying to convert "Large Text" to a number.
Try this code:
try {
String string = textView.getText().toString();
int num = Integer.parseInt(string);
textView.setText(String.valueOf(--num));
catch(NumberFormatException ignored){
}
with a try/catch block
Related
I am working on an android app development and I am stuck in an issue. I have used ANSYNC TASK method but now it has stopped working.
It was working fine from last many years but now it is creating problem.
Also, doinbackground() and postExecute() methods are not working (they are not called) only preExecute() method is working for me.
I am attaching code here for the reference:
class MyAsyncTask extends AsyncTask<Void, ConversationModel, Void> {
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
heading.setText("myheading");
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (ConversationModel model : dataList) {
if (flagStop)
break;
publishProgress(model);
long_time = Long.parseLong(model.sound_time) * 1000 + 500;
try {
Thread.sleep(long_time);
Thread.sleep(long_extraTime);
long_extraTime = 0;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
Log.d("i", i + " -----------------");
}
return null;
}
protected void onProgressUpdate(ConversationModel... model) {
if (i % 2 == 0) {
View v = View.inflate(Conversation1.this, arrInt_resource[0], null);
v.setAnimation(null);
TextView txtV_spn = (TextView) v.findViewById(R.id.textView_spn);
TextView txtV_eng = (TextView) v.findViewById(R.id.textView_eng);
ImageView img_sound = (ImageView) v.findViewById(R.id.imgsound_conv_spn2eng);
RelativeLayout speak_layout = (RelativeLayout) v.findViewById(R.id.speaking_layout);
heading.setText(dataList.get(0).heading);
if (flag != 1) {
txtV_spn.setText(model[0].eng_txt);
txtV_eng.setText(model[0].spn_txt);
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (i >= dataList.size()) {
Log.d("i", i + " ------------------");
makingcontinueImageView();
}
}
}
i think you are using .execute() method of asynctask to start execute of asynctask, change that line of code with this one
asynctask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
It is stated in d.android.com for onPreExecute() that it runs on the UI thread before doInBackground(Params...) so it should easily access the TextView and perform setText() method from the Activity from which it was executed().
But here in the below codes the loading TextView is privately declared inside the class SplashScreen that extends Activity. Inside the onCreate() it is linked with the TextView widget of the UI. But when AsyncTask extended class Atom the function onPreExecute() is executed which throws a NullPointerExcepction for the statement loading.setText("Loading..."); executed inside it.
Here the code
public class SplashScreen extends Activity implements AnimationListener{
...
TextView loading=null;
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
try {
a = (Atom) new Atom().execute(null,null,null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
}
...
loading = (TextView) findViewById(R.id.textView2);
....
}
public class Atom extends AsyncTask<RSSFeed, Void, RSSFeed>{
private RSSReader reader;
private RSSFeed feed = null;
private String uri = "http://website.com/feed/";
#Override
protected void onPreExecute() {
super.onPreExecute();
//------------problem----area-------------------
loading.setText("Loading...");
//------------problem----area-------------------
}
#Override
protected RSSFeed doInBackground(RSSFeed... arg0) {
reader = new RSSReader();
try {
feed = reader.load(uri);
Log.d("rss", feed.getTitle());
} catch (RSSReaderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return feed;
}
#Override
protected void onPostExecute(RSSFeed result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
prg.cancel();
t(result.getTitle().toString());
}
}
}
The stack:
03-09 10:50:12.793: W/System.err(14214): java.lang.NullPointerException
03-09 10:50:12.813: W/System.err(14214): at in.edu.ss.er.splash.SplashScreen$Atom.onPreExecute(SplashScreen.java:158)
03-09 10:50:12.827: W/System.err(14214): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
03-09 10:50:12.833: W/System.err(14214): at android.os.AsyncTask.execute(AsyncTask.java:534)
03-09 10:50:12.833: W/System.err(14214): at in.edu.ss.er.splash.SplashScreen.onCreate(SplashScreen.java:45)
Try to initialize TextView before executing asyntask. Like following.
try {
loading = (TextView) findViewById(R.id.textView2);
a = (Atom) new Atom().execute(null,null,null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
}
I don't know this is correct or not, This my guessing, So, Please let me know what happened.
Thanks
just initialize your text view before calling AsyncTask. do something like this
loading = (TextView) findViewById(R.id.textView2);
try {
a = (Atom) new Atom().execute(null,null,null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
}
You have to initialize your textview before then call asynctask. Change your code into following-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
loading = (TextView) findViewById(R.id.textView2);
try {
a = (Atom) new Atom().execute(null,null,null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
finish();
}
}
I am making an application where i need to take the input from the user and append that input with the string and use that as a url for data parsing
But the edit text content is null even though i am entering text in edittext
I converted edittext content to string as below
EditText edit = (EditText)findViewById(R.Id.tv5);
And inside onclicklistener
String data = edit.getText().toString();
Can anybody tell me why the data.length() is giving me zero?
my complete main activity is below:
public class Pnr extends Activity {
EditText edit;
TextView text1;
Button button;
String pnr, check;
HttpClient client;
JSONObject json;
int s;
String URL = "pnrbuddy.com/pnrstatus=";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pnr);
edit = (EditText) findViewById(R.id.tv5);
text1 = (TextView) findViewById(R.id.textView2);
button = (Button) findViewById(R.id.button1);
client = new DefaultHttpClient();
pnr = String.valueOf(edit.getText());
s = pnr.length();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Read().execute("trainNo");
}
});
}
public JSONObject pnrStatus(String key) throws ClientProtocolException,
IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(key);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
JSONObject last = new JSONObject();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
last = new JSONObject(data);
return last;
} else {
return last;
}
}
public class Read extends AsyncTask<String, Integer, String> {
#Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = pnrStatus(pnr);
return json.getString(arg0[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return s + "";
}
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
text1.setText(" " + result);
}
}
}
when i enter the string instead of accepting from edittext in the string URL along with the exsisting string the code works
URL="pnrbuddy/pnrstatus=myText";
why i m unable to get string from editText?
try with this
EditText edit = (EditText)findViewById(R.id.tv5);
String data = edit.getText().toString();
If you are try to get length in onclicklistener in EditText it will give u 0 only.In this case try to get from onTextChange.
Replace your on create method by this n try
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pnr);
edit = (EditText) findViewById(R.id.tv5);
text1 = (TextView) findViewById(R.id.textView2);
button = (Button) findViewById(R.id.button1);
client = new DefaultHttpClient();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
pnr =edit.getText().toString();
s = pnr.length();
new Read().execute("trainNo");
}
});
}
Actually your getting text when the activity is created not when button is clicked, So placing the gettext code in onclick method would resolve your problem
I am using Jsoup to parse a part of a website and then put it into a string. I want to visualize this string into a textView, but since only the thread that had created the textView can modify it i need to pass the value of the string into the main thread. how?
This is the code: (ignore the tabhost stuff)
public class NewsAndAnnouncements extends Activity {
TabHost host;
FlyOutContainer container;
Button bttoggle;
Button bt1;
String loggity;
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.container = (FlyOutContainer) this.getLayoutInflater().inflate(
R.layout.newsandannouncements, null);
this.setContentView(container);
host = (TabHost) findViewById(R.id.tabhost);
host.setup();
TabSpec specs = host.newTabSpec("TAGGITY EINZ");
specs.setContent(R.id.tab1);
specs.setIndicator("News");
host.addTab(specs);
specs = host.newTabSpec("TAGGITY ZWEI");
specs.setContent(R.id.tab2);
specs.setIndicator("Notices");
host.addTab(specs);
specs = host.newTabSpec("TAGGITY DREI");
specs.setContent(R.id.tab3);
specs.setIndicator("Events");
host.addTab(specs);
tv1 = (TextView) findViewById(R.id.textView1);
/*
* bttoggle = (Button) findViewById(R.id.bttoggle); bt1 = (Button)
* findViewById(R.id.Button1);
*
* bttoggle.setOnClickListener(new OnClickListener() {
*
* #Override public void onClick(View v) { // TODO Auto-generated method
* container.toggleMenu(); } });
*
* bt1.setOnClickListener(new OnClickListener() {
*
* #Override public void onClick(View v) { // TODO Auto-generated method
* container.toggleMenu(); } });
*/
Thread newsThread = new Thread() {
public void run() {
Document doc = null;
try {
doc = Jsoup
.connect(
"http://acs.bg/Home/About_ACS/News_and_Events/News.aspx")
.get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Elements myin = doc.getElementsByClass("news_list");
loggity = myin.toString();
Log.i("ELEMENTS HTML", loggity);
}
};
newsThread.start();
tv1.setText(loggity);
}
}
Try using AsyncTask instead of the Thread. To modify views on your ui thread use the runOnUiThread() method in your activity.
runOnUiThread(new Runnable() {
#Override
public void run() {
tv1.setText("...");
}
});
Use an AsyncTask instead of a raw Thread:
new AsyncTask<URL, Object, Document>() {
protected Document doInBackground(URL... urls) {
// parse URL and return document
}
protected void onPostExecute(Document result) {
// this runs in UI thread
// show document in UI
}
}).execute(myURL);
There are two ways to d it-
1)- Using AsyncTask
2)- Using Handler
Thread newsThread = new Thread()
{
public void run()
{
Document doc = null;
try {
doc = Jsoup
.connect(
"http://acs.bg/Home/About_ACS/News_and_Events/News.aspx")
.get();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Elements myin = doc.getElementsByClass("news_list");
loggity = myin.toString();
mHandler.post(new Runnable()
{
#Override
public void run()
{
try
{
tv1.setText(loggity);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
Log.i("ELEMENTS HTML", loggity);
}
};
newsThread.start();
You can initialize the Hanlder in the start.
try this sample code, don't know if this is the better way:
public class MainThread {
public static void main(String args[]) {
Thread2 t2 = new Thread2();
Thread nextThread = new Thread(t2);
nextThread.start();
try {
nextThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
System.out.println(t2.getStr());
}
private static class Thread2 implements Runnable{
private String str;
#Override
public void run() {
setStr("T2 Thread String");
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
}
I send a string from android to C# and from C# to android
it works on simulator but does't work on real android device
My phone using android 2.3
here is my code on Android:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hienthi_lv_tab2 = (ListView) findViewById(R.id.lvhienthi);
chuoi = (EditText) findViewById(R.id.string);
ipserver = (EditText) findViewById(R.id.ipserver);
txt = (TextView) findViewById(R.id.Show);
quit = (Button) findViewById(R.id.quit);
connect = (Button) findViewById(R.id.Connect);
send = (Button) findViewById(R.id.Send);
quit.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
OUT_message = chuoi.getText().toString();
new Thread(new Runnable() {
final Handler handler = new Handler();
final Runnable updateUI2 = new Runnable() {
public void run() {
// TODO Auto-generated method stub
OUT_message = "Android: " + OUT_message;
add_chuoi(OUT_message);
}
};
public void run() {
// TODO Auto-generated method stub
PrintWriter out;
try {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())), true);
out.println(OUT_message);
out.flush();
handler.post(updateUI2);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
Thread C_thread = new Thread(new ClientThread());
C_thread.start();
}
});
connect.setVisibility(1);
connect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
connect.setVisibility(v.GONE);
Thread C_thread = new Thread(new ClientThread());
C_thread.start();
}
});
}
public class ClientThread implements Runnable {
final Handler handler = new Handler();
final Runnable updateUI = new Runnable() {
public void run() {
// TODO Auto-generated method stub
txt.setText(IN_message);
IN_message = "Server: " + IN_message;
add_chuoi(IN_message);
}
};
public void run() {
// TODO Auto-generated method stub
try {
IP = ipserver.getText().toString();
serverAddr = InetAddress.getByName(IP);
state_connnect = true;
Scanner in;
while (state_connnect) {
s = new Socket(serverAddr, 4444);
in = new Scanner(s.getInputStream());
IN_message = in.nextLine();
handler.post(updateUI);
in.close();
s.close();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void add_chuoi(String chuoi) {
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array_operator);
array_operator.add(chuoi);
hienthi_lv_tab2.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
here is my code on C#:
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
String hostname = "";
System.Net.IPHostEntry ip = new IPHostEntry();
hostname = System.Net.Dns.GetHostName();
ip = System.Net.Dns.GetHostByName(hostname);
lblhostname.Text = "Tên Server : " + ip.HostName;
foreach (System.Net.IPAddress listip in ip.AddressList)
{
lblip.Text ="Địa Chỉ IP Server : "+listip.ToString();
IPAddress ipAd = IPAddress.Parse(listip.ToString());
myList = new TcpListener(ipAd, 4444);
myList.Start();
}
}
void connect()
{
txt_show.Text="Waitting for connect from android...";
while (true)
{
server_socket = myList.AcceptSocket();
byte[] data_rec = new byte[1024];
int k = server_socket.Receive(data_rec);
char cc;
String mes = null;
for (int i = 0; i < k - 1; i++)
{
cc = Convert.ToChar(data_rec[i]);
mes += cc.ToString();
}
if (mes != null)
{
string_rec = mes.ToString();
txt_show.Text += Environment.NewLine + "Android: " + mes;
}
}
}
private void btn_send_Click(object sender, EventArgs e)
{
String data_send = txt_send.Text;
ASCIIEncoding asen = new ASCIIEncoding();
server_socket.Send(asen.GetBytes(data_send));
txt_show.Text += Environment.NewLine + "Server : " + txt_send.Text;
txt_send.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
S_thread = new Thread(connect);
S_thread.Start();
}
and this is logcat:
FATAL EXCEPTION:Thread-10 java.util.NoSuchElementException
at java.util.Scanner.nextLine(Scanner.java:1417)
at iNET.Android.Thread_TCP.ThreadActivity$ClientThread.run(ThreadActivity.java:207)
at java.lang.Thread.run(Thread.java:1019)
thanks all!
Not fully sure, But my guess is you are using different private network in Mobile and Pc. That's why you can send data while using emulator ( because both in pc) but not able to send using mobile.
After your comments I think your pc is in a private network. So you can not send message from the android application using the entered ip.