How to access arraylist outside the oncreate - java

Hi i am new to programming. I want access arraylist outside from the onCreate but i get error.Below is code.
public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
List<UserDate> data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;// here i get error
}
}

Just declare the list globally at class level. In your case before OnCreate.
List<UserDate> data= new ArrayList();
en#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;
}
Hope it will help.

public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
List<UserDate> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;// here i get error
}
}

public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
final List<UserDate> data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle(data);
}
});
}
private void handle(List<UserDate> listUD){
String info=listUD.getUserInfo;// here i get error
}
}

Related

Android: How can I use ringtone.stop in activity by click button

I have Mybroadcastreceiver and when rintone is play stopAlarm.java page is shown If button is pressed i want to use stop ringtone. How can I use r.stop() in my stopAlarm.java
public class MyBroadcastReceiver extends BroadcastReceiver {
public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
private final static String default_notification_channel_id = "default" ;
#Override
public void onReceive(Context context, Intent intent) {
Uri notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r = RingtoneManager.getRingtone(context,notificationsound);
r.play();
Intent i = new Intent(context,stopAlarm.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
public class stopAlarm extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_alarm);
Button stop = (Button)findViewById(R.id.stopAlarm);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//////////How can I get r
}
});
}
}
public class stopAlarm extends AppCompatActivity {
Ringtone r;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_alarm);
Button stop = (Button)findViewById(R.id.stopAlarm);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
r.stop();
}
});
}

Custom listener with interface

I am trying to understand better how to create a custom listener with a simple example but I don't know how to start the interface so that it is not null:
public class MainActivity extends AppCompatActivity implements ListenerButton{
TextView helloToOther;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helloToOther = findViewById(R.id.helloWorldToOtherActivity);
helloToOther.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ButtonActivity.class));
}
});
}
#Override
public void onClickButton(View view) {
Toast.makeText(this, "Estoy en la primera activity", Toast.LENGTH_SHORT).show();
}
}
This is the second activity:
public class ButtonActivity extends AppCompatActivity {
Button btnInterface;
ListenerButton listenerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
btnInterface = findViewById(R.id.button_activity__btn__button_interface);
setUpButtonInterface();
}
private void setUpButtonInterface() {
btnInterface.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listenerButton.onClickButton(v);
}
});
}
}
And there's the interface:
public interface ListenerButton {
void onClickButton(View view);
}
Basically I get a null pointer exception on the second activity because the interface is null, but I don't fall right now as I can start it. Thank you very much.
the reason you are getting a null pointer exception is because you haven't assigned anything to variable listenerButton and therefor it is in fact null!!
you don't need a new interface for that you just need to do this:
public class ButtonActivity extends AppCompatActivity {
Button btnInterface;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
btnInterface = findViewById(R.id.button_activity__btn__button_interface);
setUpButtonInterface();
}
private void setUpButtonInterface() {
btnInterface.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do whatever you want to do when button is clicked!
}
});
}
}
if you want to define whatever you want to do when button is clicked you need a class and not an interface:
public class ButtonListener implements View.OnClickListener{
#Override
public void onClick(View v) {
//do whatever you want to do when button is clicked!
}
}
and then in your activity do this:
public class ButtonActivity extends AppCompatActivity {
Button btnInterface;
ListenerButton listenerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
listenerButton = new ButtonListener();
btnInterface = findViewById(R.id.button_activity__btn__button_interface);
setUpButtonInterface();
}
private void setUpButtonInterface() {
btnInterface.setOnClickListener(listenerButton);
}
}
Create a new file:
MyListener.java:
public interface MyListener {
// you can define any parameter as per your requirement
public void callback(View view, String result);
}
In your activity, implement the interface:
MyActivity.java:
public class MyActivity extends Activity implements MyListener {
#override
public void onCreate(){
MyButton m = new MyButton(this);
}
// method is invoked when MyButton is clicked
#override
public void callback(View view, String result) {
// do your stuff here
}
}
In your custom class, invoke the interface when needed:
MyButton.java:
public class MyButton {
MyListener ml;
// constructor
MyButton(MyListener ml) {
//Setting the listener
this.ml = ml;
}
public void MyLogicToIntimateOthers() {
//Invoke the interface
ml.callback(this, "success");
}
}

Android : How can I use method emitBubbles() in Question1 class

I'm doing a quiz and I don't want to write in all of new Activities this method but I can't make it work. I was trying to make another java class to put there all methods but how do that
Hope u gonna help me guys
//Main class
public class MainActivity extends AppCompatActivity {
private Button button;
BubbleEmitterView bubbleEmitter;
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emitBubbles();
bubbleEmitter = findViewById(R.id.bubbleEmitter);
button = findViewById(R.id.startButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startQuiz();
}
});
}
public void emitBubbles()
{
handler.postDelayed(new Runnable() {
#Override
public void run() {
//determine the size of the bubbles and the range
int size = new Random().nextInt(81)+20;
bubbleEmitter.emitBubble(size);
emitBubbles();
}
},
//determine the range for the bubbles to appear
new Random().nextInt(301)+100);
}
//question class
public class Question1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
}
}

Can i declare and use the same button in an Activity and a class?

I want to use the button for different calls in the different classes. However, when I declare and try to call the Button click method in the Activity class, it throws a null exception. In my Class I want to do this:
public class CustomFeedListViewAdapter extends BaseAdapter {
holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);
holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
private class ViewHolder {
Button feedUpVoteButton;
}
And in my main activity I want to do this:
public class Feed extends AppCompatActivity {
Button upVoteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
upVoteButton = (Button) findViewById(R.id.feedUpVoteButton);
feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
}
}
You are getting NullPointerException because you are trying to access feedUpVoteButton which is present in the item layout used in CustomFeedListViewAdapter and not in the layout used by Feed activity.
Do you want to have access to the ClickListener whenever you create an adapter of type CustomFeedListViewAdapter? If yes then do this...
Your Activity class
public class Feed extends AppCompatActivity {
CustomFeedListViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
adapter = new CustomFeedListViewAdapter( new OnClickListener() {
#Override
public void onClick(View v) {
// Handle click here
}
});
}
}
And your custom adapter class
public class CustomFeedListViewAdapter extends BaseAdapter {
View.OnClickListener clickListener;
// Constructor
public CustomFeedListViewAdapter(View.OnClickListener listener) {
clickListener = listener;
}
#Override
void getView(){
holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);
holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
}
}
This way you can set a different listener for each instance of the adapter you create.

Error : onCreate(Bundle) is already defined in this activity

Being new to Android Application development i was trying to learn connecting two activities using Intent. I tried a code from a book. It keeps throwing an error saying - 'onCreate(Bundle)' is already defined in MainActivity class as well as the NewActivity class. Would be of great help if i could get a solution.
MainActivity.class
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});
}
NewActivity.class
public class NewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}
}
If you want to connect those activities you have to do this :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});
}
And then in yout second activity just delete the:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}
And copy this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
And it will work.
Just change your NewActivity to:
public class NewActivity extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}
}
A class can contain only one onCreate() method.First learn about Activity Life Cycle http://developer.android.com/training/basics/activity-lifecycle/starting.html
Just remove the first onCreate event on your main activity and new activity. you don't need twice
public class MainActivity extends Activity {
#Override
/*protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}*/
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});
}

Categories