Hi I'm beginner with android development and I have a problem with my following test code:
Main Activity:
package com.test.thread;
import java.util.concurrent.ExecutionException;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txt = null;
Button btStart = null;
Button btStop = null;
TestClass test = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.txtView);
btStart = (Button)findViewById(R.id.bt_start);
btStop = (Button)findViewById(R.id.bt_stop);
test = new TestClass(this, txt);
btStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
test.Stop();
}
});
btStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread t = new Thread(){
#Override
public void run() {
// TODO Auto-generated method stub
try{
synchronized (this) {
wait(50);
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
while(test.isStop()){
try {
wait(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
test.DoSomeThing();
}
}
});
}
}catch( InterruptedException e)
{
e.printStackTrace();
}
}
};
t.start();
}
});
}
#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);
}
}
TestCLas.java:
package com.tutorial.sample;
import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;
public class TestClass{
private boolean stopIter = false;
private TextView txtV = null;
private int count = 0;
private Context ctx = null;
public TestClass(Context ct, TextView tv) {
// TODO Auto-generated constructor stub
txtV = tv;
stopIter = false;
count = 0;
ctx = ct;
}
public boolean isStop() { return stopIter; }
public void Stop()
{
Toast.makeText(ctx, "stop the test" , Toast.LENGTH_LONG).show();
count = 0;
txtV.setText(Integer.toString(count));
stopIter = true;
}
public void DoSomeThing(){
txtV.setText(Integer.toString(count));
}
}
I want to manage my activity's viewer with threads. I launch a thread in order to modify TextView's text (each sec) when the start button is clicked and stop it when stop button is clicked. But it does not work, it crashes without any exception. Can someone help me please?
wait(1000);
This should NEVER be done on the UI thread.
The UI thread is the one that update the GUI, and communicates with Android to say everything is working as expected. When you have a thread delay on the UI thread, the message to and from the android system get held back, and you app appears to the system as if it has crashed.
Long story short I an trying to make an app to convert Celsius to Fahrenheit and back. I want to get input from an edit text so I can use that for my math, but I can't figure it out, nor can I get the button to work right to close it. I want to use an internal anonymous class to do the calculations and read in the value, but again no idea what to do. Does anyone have any idea where to start?
here is what I have
package com.example.a4;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements TextWatcher {
EditText mt=(EditText) findViewById(R.id.et1),
mt2=(EditText) findViewById(R.id.et2);
Button bt = (Button) findViewById(R.id.btn1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//mt=(EditText) findViewById(R.id.et1);
// mt2=(EditText) findViewById(R.id.et2);
}
OnClickListener oclBtnOk = new OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
};
/*
buttonExit.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
System.exit(0);
}
}
);*/
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
int c = Integer.parseInt(s.toString());
ftoc(c);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
void ftoc(int c){
int f = ((9/5)*c) + 32;
mt2.setText(f);
}
/*
#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;
}
*/
}
You need to move your mt = (EditText)findViewById stuff back to onCreate. Then, if you want to use anonymous classes, do something like:
mt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
...
}
});
Also, make sure you're using View.OnClickListener and not DialogInterface.OnClickListener as you are now (check your imports).
I get that error message everytime I try running my program. I have a fairly simple program, with two buttons. If you press one button it subtracts 1 from a number. If you press the add button it adds 1 to the number. I have no compiling errors and I'm fairly new to android. Any help would be great... Thanks!
Here is my code: (The main activity).
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView numberText = (TextView) findViewById(R.id.number);
numberText.setText(String.valueOf(0));
Button.OnClickListener listenerAdd = new Button.OnClickListener() {
#Override
public void onClick(View v) {
TextView numberText = (TextView) findViewById(R.id.number);
switch(v.getId()) {
case R.id.HomeScreenChangeAdd: {
String currText = numberText.getText().toString();
int curNumb = Integer.parseInt(currText);
int newNumb = curNumb +1;
String newText = String.valueOf(newNumb);
numberText.setText(newText);
break;
}
}
}
};
Button.OnClickListener listenerSub = new Button.OnClickListener() {
#Override
public void onClick(View v) {
TextView numberText = (TextView) findViewById(R.id.number);
switch(v.getId()) {
case R.id.HomeScreenChangeSub: {
String currText = numberText.getText().toString();
int curNumb = Integer.parseInt(currText);
int newNumb = curNumb -1;
String newText = String.valueOf(newNumb);
numberText.setText(newText);
break;
}
}
}
};
((Button) findViewById(R.id.HomeScreenChangeAdd)).setOnClickListener(listenerAdd);
((Button) findViewById(R.id.HomeScreenChangeSub)).setOnClickListener(listenerSub);
}
#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;
}
}
Make sure
R.id.number,
R.id.HomeScreenChangeAdd, and
R.id.HomeScreenChangeSub
are included in R.layout.activity_main.
There is nothing else in your code that could crash the app at startup.
I'm starting learning android development but im facing a an error which says :
Multiple markers at this line
- view cannot be resolved to a type
- The method setOnClickListener(View.OnClickListener) in the type View is not applicable
for the arguments (new
OnClickListener(){})
my program is :
package com.sc.uploader;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add;
Button sub;
TextView disply;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add =(Button) findViewById(R.id.badd);
sub=(Button) findViewById(R.id.bsub);
disply= (TextView) findViewById(R.id.tvdisplay);
add.setOnClickListener(new view.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#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;
}
}
so please if you could help me resolving the error i will be grateful
thanks
Change this line
add.setOnClickListener(new view.OnClickListener() {
to
add.setOnClickListener(new View.OnClickListener() {
capital "V". It is looking for a variable view when it should be set on the View Class.
OnClickListener Docs
I am getting an error in 2 places and I don't know why as I am new to Android...
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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 onTouchEvent(MotionEvent event)
{
Context context=getApplication();
CharSequence text = "This is for ontouch event";
int duration = Toast.LENGTH_SHORT;
Toast Msg = Toast.makeText(context,text,duration);
int x=(int)event.getX();
int y=(int)event.getY();
Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
Msg.show();
return true;
} //Error type Syntax error on token "}", delete this token
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
} // Error Syntax error, insert "}" to complete ClassBody
Write your TextView code in onCreate() method, you can't write this code outside any function the way you have written.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
}
Move this inside onCreate
TextView t1=(TextView)findViewById(R.id.text);
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this ,"Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
Paste the entire code and try this
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Text View Clicked",Toast.LENGTH_LONG).show();
return false;
}
});
}
#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 onTouchEvent(MotionEvent event)
{
Context context=getApplication();
CharSequence text = "This is for ontouch event";
int duration = Toast.LENGTH_SHORT;
Toast Msg = Toast.makeText(context,text,duration);
int x=(int)event.getX();
int y=(int)event.getY();
Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
Msg.show();
return true;
//Error type Syntax error on token "}", delete this token
} //
}
This should definitely work and click the text for a long time .Sure it will give you the output.
Try this code...
class file...
package com.longclick;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
}
//#Override
//public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_main, menu);
//return true;
//}
#Override
public boolean onTouchEvent(MotionEvent event)
{
Context context=getApplication();
CharSequence text = "This is for ontouch event";
int duration = Toast.LENGTH_SHORT;
Toast Msg = Toast.makeText(context,text,duration);
int x=(int)event.getX();
int y=(int)event.getY();
Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
Msg.show();
return true;
} //Error type Syntax error on token "}", delete this token
}
.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
</RelativeLayout>
I think it is working for you...
demo here
This code is outside of any function:
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
Check your functions structure.
The last two paragraphs of your code need a function to wrap them, possibly onCreate.
You need to call setLongClickable on the view for the long click to register.
Try this,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.textView1);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Text View Clicked",Toast.LENGTH_LONG).show();
return false;
}
});
}
Click the text for a long time and don't leave it quickly.
It is working fine.