package com.example.harikrishna.myapp4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText ed1,ed2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
}
public void fun(View V){
if(V.getId()==R.id.button){
Editable e=ed1.getText();
String s=e.toString();
s=s.trim();
if(s.length()>0){
ed2=setText(s);
ed1=setText("");
ed2=requestFocus();
}
}
else{
String s=ed2.getText().toString().trim();
if(s.length()>0){
ed1=setText(s);
ed2=setText("");
ed1=requestFocus();
}
}
}
}
compiler shows error Error:(26, 21) error: cannot find symbol method setText(String) and cannot resolve method setText(java.lang.String).
pl could u pl some one let me know how to solve this problem
change your code from
ed2=setText(s);
ed1=setText("");
ed1=requestFocus();
to this
ed2.setText(s);
ed1.setText("");
ed1.requestFocus();
Related
Code showing uplisted error while adding clock to the fragmented activity.
Error Message displayed is - error: incompatible types: Schedule_fragment cannot be converted to Fragment.
I am new to this and have written this code from a youtube tutorial, now trying to merge with my code. Please help.
package com.example.fastfill.ui.main;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import com.example.fastfill.R;
public class Schedule_fragment extends Fragment implements TimePickerFragment.TimePickerListener {
private TextView displaytime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule_fragment);
displaytime=findViewById(R.id.displaytime);
Button showtime=findViewById(R.id.showtime);
showtime.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
DialogFragment timePickerFragment=new TimePickerFragment();
timePickerFragment.show(getSupportFragmentManager(),"timePicker");
}
});
}
#Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
displaytime.setText("Hour = " +hour + "Minute = "+ minute);
}
}
You are extending AppCompatActivity, if you want to use Schedule_fragment as a fragment, you need to extend Fragment.
I keep getting this error:
The method setOnClickListener(View.OnClickListener) in the
type View is not applicable for the arguments (new OnClickListener(){})
Code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button new_claim_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new_claim_button = (Button) this.findViewById(R.id.button1);
new_claim_button.setOnClickListener(new OnClickListener() {
}
});
}
}
Please ignore functionality. It does not do much, but I need to figure out what is causing the error before I can continue.
As the comments say, your brackets don't match, and you're missing method implementation. Correct listener should override onClick(), as below:
new_claim_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//implementation
}
});
So I just started programming in eclipse, and I got the error ``main cannot be resolved or is not a field" with this code:
package com.example.wouter1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.wouter1.R;
public class HelloWorldActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // PROBLEM HERE <<<< # MAIN
TextView myText = new TextView(this);
myText.setText("hello world");
setContentView(myText);
}
}
I wanted to navigate to a new page when clicking a button. Below are my both activities I have created. I am using eclipse and I am getting an error saying The method onClick(View) of type new View.OnClickListener(){} must override a superclass method on the main activity.
This is my main activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button grammerButton = (Button)findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
This is my second activity.
package com.example.grammer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grammer);
Button grammerButton = (Button) findViewById(R.id.grammar);
grammerButton.setOnClickListener(new View.OnClickListener() {
// #Override
public void onClick(View view) {
finish();
}
});
}
}
Removing the #override will remove the error, but then app is not working as intended.
Check this
OnClickListener() must override a superclass method?
Uncheck "Enable project specific settings", click "Configure Workspace Settings..." and change "Compiler Compliance Level" to 1.6 or above
Have this import statement
import android.view.View.OnClickListener;
There is no need to remove #Override Annotation.
Also calling finish() is not necessary. The hardware back button does the job.
When you press back button in Grammar Activity your current activity is popped from the back stack and the previous activity in the back stack takes focus. So there is no need to call finish() on button click.
http://developer.android.com/guide/components/tasks-and-back-stack.html
Also if you have a Button with id grammar in activity_grammer.xml it is ok.
Make sure you have a button with id grammar in activity_grammer.xml
You can read the topic id
http://developer.android.com/guide/topics/ui/declaring-layout.html
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching.
Change "Compiler Compliance Level" to 1.6. of java from project properties.
Place this code::
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alayout);
Button grammerButton = (Button)findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Grammer.class);
startActivity(intent);
}
});
}
}
Just replace then name of layout and button id by your
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Grammer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blayout);
Button grammerButton = (Button) findViewById(R.id.aId);
grammerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
You are suppose to import the ViewClickListener namespace in your code.
Just Press ctrl + shift + O and it will add the relevant and missing namespaces in your project.
All you need is to import the library for OnClickListener. Just press ctrl + Shift + O in your eclipse and it will import the import android.view.View.OnClickListener file for you.
Try this..
First, You havn't import OnClickListener
import android.view.View.OnClickListener;
and second one
Button grammerButton = (Button) findViewById(R.id.grammar);
you are giving same name for both Button Ids in different layouts. Make sure you have a button with id grammar in activity_grammer.xml present are not.
I am wondering why image view in java class pots error
like ImageView smtin;
And there's error.
It dont recognize it like class...
My project code is:
package com.klemenjezakon.koceSLO;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
ImageView slika;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent i = getIntent();
koca podatki=(koca)i.getSerializableExtra("koca");
setContentView(R.layout.koca);
}
}
You have to put inside your class. So instead of:
ImageView slika;
public class MainActivity extends Activity {
it have to be:
public class MainActivity extends Activity {
ImageView slika;