onSwipe(int direction) not working - android

I can't override the onSwipe() method. The error is "The method onSwipe(int) of type Adds must override or implement a supertype method". Can anyone tell me what I did wrong?. I want to navigate between activities using swipe gesture. Is there any other way to do it? If so please explain. Do I have to import any more packages?
package com.mahavega.qcdemo;
import com.mahavega.qcdemo.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class Adds extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ads);
TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Adds.this, Ads2.class));
}
});
ImageView im1 = (ImageView) findViewById(R.id.imageView1);
im1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Adds.this, Real.class));
}
});
}
#Override
public void onSwipe(int direction) {
Intent intent = new Intent();
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT:
intent.setClass(this, Ads2.class);
break;
case SimpleGestureFilter.SWIPE_LEFT:
intent.setClass(this, Ads3.class);
break;
}
startActivity(intent);
}
}

your activity should implement SimpleGestureListener to be able to recieve gesture events.
public class Adds extends Activity implements OnGestureListener

Your error reads from the fact that Activity does not have an onSwipe(int) method. So the error is stating that you cannot override a method that has no super method.
Also as #sokie said, check out the OnGestureListener from the link he added.As for swiping gesture used to start new activities,
when swiping left to right, override call onBackPressed (like you are going back) and on swipe right to left start new activity. Although this means you have to create the gesture listener in each of your activities.

Related

My code won't recognise my class when navigating to a second screen

I'm really stuck here. I'm trying to navigate to a second activity using a button but whenever i try to parse in the name of the class to the Intent method, Android Studio throws an error.
In the Intent method toWeightsScreen, it won't let me parse in a class.
Can anyone tell me what I'm messing up please.android studio snapshot
package leith.comstephen.facebook.httpswww.fitnessapp5;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button navToWeightsScreen = (Button)findViewById(R.id.Firstweights);
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(this, cut.class)
startActivity(toWeightsScreen);
}
});
}
}
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(getActivity(), cut.class);
startActivity(toWeightsScreen);
}
});
The this in your intent declaration refers to the button's setOnClickListener.
You should specify the actual activity. See the code below
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Changed cut.class to Cut.class to follow coding conventions
Intent toWeightsScreen = new Intent(MainActivity.this, Cut.class);
startActivity(toWeightsScreen);
}
});

Method does not override another method View.OnClickListener (Error in Override)

I m trying to create a new java file for button (sin) as this is about calculator. But it is showing Error in second override annotations. (Method does not override method from its SuperClass). There is one more Error associating with brackets in the bottom part of code. Being newbie this is the first time I'm having my hands on this part of java. I have searched many of the sites but didn't found any solution. Any help will be appreciated.
SinglesinActivity.java
package com.marsh.calculator.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class SinglesinActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.btnsin);
b.setOnClickListener(this);
}
#Override
public void onclick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnsin:
break;
}
};
}
It's onClick not onclick.
Make the following changes:
#Override public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnsin: break;
}
};
Its a typo. change onclick to onClick.

Trying to access functions from MainActivity

I am trying to access MainActivity function to my another java class. But i am not able to use these function. Please tell me what else need to be added to get it access.
My code:
Where i am trying to access my MainActivity
package com.example.musicplayer;
**import com.example.musicplayer.MainActivity;**
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class current_song extends Activity implements OnClickListener{
MainActivity ma = new MainActivity();
protected void onCreate(Bundle icicle) {
Bundle extra = getIntent().getExtras();
super.onCreate(icicle);
setContentView(R.layout.songplay_page);
if(extra != null){
String song_name = extra.getString("song_name");
TextView textchange = (TextView)findViewById(R.id.current_song_name);
textchange.setText(song_name);
textchange.setSelected(true);
}
Button btn_pause = (Button)findViewById(R.id.pause_btn);
btn_pause.setOnClickListener(this);
Button btn_next = (Button)findViewById(R.id.next_btn);
btn_next.setOnClickListener(this);
Button btn_prv = (Button)findViewById(R.id.prv_btn);
btn_prv.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "In Onclick ()", Toast.LENGTH_SHORT).show();
switch(v.getId())
{
case R.id.pause_btn:
Toast.makeText(getApplicationContext(), "pause", Toast.LENGTH_SHORT).show();
ma.pause();
break;
case R.id.next_btn:
ma.next();
break;
case R.id.prv_btn:
ma.prv();
break;
}
}
}
Make sure that MainActivity has a zero argument constructor and the access specifier for pause , next and prv function is public.
In response to "i have some methods defined by me stop(), next(), pri() i am trying to access these methods when i click on each button. If you think that "creating a separate common class for sharing all methods" can you please show me 1 example bec i don't know how to access a method from 1 activity to another. "
public class myController{
private MyActivity m;
public myController(MyActivity m){
this.m = m;
}
public void stop(){
m.stop;
}
}
In other classes you initialize in the main activity and pass it the controller object so it can call the stop method

onClickListener not working that was copied from another working activity

I have created a new activity, added it to my manifest file and copy and pasted code from another fully functioning activity yet my buttons do not work when I click on them. Here is my activity:
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class test extends Activity {
private Button btnChangeDate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.salesticketoilui);
mainProgram();
}
public void mainProgram() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
} // end onClick
}); // end setOnClickListener
Button buttonExit = (Button)findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
exitActivity();
} // end onClick
}); // end OnClickListener
// setup button listener for saving data and exit to main
Button buttonSaveExit = (Button) findViewById(R.id.buttonSaveExit);
buttonSaveExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveExit();
} // end onClick
}); // end OnClickListener
} // end MainProgram ()
public void saveExit() {
// does stuff
}
public void exitActivity () {
// does stuff
}
} // end class
any thoughts?
Based on the code you have shown, it doesn't appear that you ever call the method mainProgram so your click listeners will never actually get setup. Either call mainProgram from onCreate or just put that code directly into onCreate.
I believe the onClickListeners need to go inside of your onCreate method.
Listen to Scott
and looks like your missing the #Override
new View.OnClickListener() {
#Override
public void onClick(View view) {
exitActivity();
} // end onClick
}
Make sure your java settings are to 1.6 to avoid code completion missing this.
I had the same problem copying OnClickListener with ImageButtons from a class to another class, and renaming then with bulk copy/paste.
To make it work, I had to create new buttons in my layout and declare the events manually. Weird!

Inherited method seems to be disowned by its ancestor

I'm still getting, "The type OnDemandAndAutomatic_Activity must implement the inherited abstract method View.OnClickListener.onClick(View)"
even though I've implemented the method in two places (placed in both places via "Quick Fix").
This is my code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class OnDemandAndAutomatic_Activity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ondemandandautomatic_activity);
// try commenting the button code out to see if that lets it run...
Button buttonAuthorizeUsers = (Button) findViewById(R.id.buttonAuthorizeUsers);
buttonAuthorizeUsers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent configure = new Intent(OnDemandAndAutomatic_Activity.this, Configure_Activity.class);
OnDemandAndAutomatic_Activity.this.startActivity(configure);
}});
}
/* #Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent configure = new Intent(OnDemandAndAutomatic_Activity.this, Configure_Activity.class);
OnDemandAndAutomatic_Activity.this.startActivity(configure);
}*/
}
Since OnDemandAndAutomatic_Activity claims to implement View.OnClickListener, you need to have the onClick() implementation that you have commented out, otherwise it will not compile.
Also, you are separately presently creating an anonymous inner class instance of View.OnClickListener that you are passing to setOnClickListener(). It too will need an implementation of onClick().
If you are thinking that you should only need one of these, then either remove implements View.OnClickListener from your class declaration or pass this to setOnClickListener().

Categories

Resources