how to get onclick method to work outside of onCreate()? - android-studio

I am confused here... let's say that you have a new class and would like to add a clickable image button to that class, can that be done? I read somewhere that it needs to be in the onCreate method, which is only found in the activity_main xml file though......
I am also having another error, where I can't find the solutions. I am having a zero constructor error in my second class when trying to use the intent method to take the user from the first class to the second class, would appreciate some assistance here as it is taking me too long to find the answers...
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PaintView pv = new PaintView(this);
setContentView(R.layout.activity_main);
Button mButton = (Button) findViewById(R.id.practice);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, PaintView.class));
}
});
My PaintView class is too long, just wondering if you could suggest which part of the code I should correct?
This is the error message:
Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
I put the setOnClickListener under my MainActivity.java and also tried it under the PaintView.java class but none is working...

Related

I want make my botton animate on fragment but i get an error in the load animation

I want ass my application screen comes on my bottons to animate from button to place i know how to do it in an activity but in a fragment it having issues.
There is an error in the this that says this cannot be applied to fragment and when i put get context it does not animate. Please tell me what wrong.
This is my main fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_main, container, false);
samabtn=(Button) view.findViewById(R.id.samabtn);
samabtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity mainActivity=(MainActivity)getActivity();
mainActivity.loadParrablesFragment();
}
});
Lovebtn=(Button) view.findViewById(R.id.Lovebtn);
Lovebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity mainActivity=(MainActivity)getActivity();
mainActivity.loadChapter2Fragment();
}
});
Adventurebtn=(Button) view.findViewById(R.id.Adventurebtn);
Adventurebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity mainActivity=(MainActivity)getActivity();
mainActivity.loadAdventureFragmnet();
}
});
menbtn=(Button)view.findViewById(R.id.menbtn);
frombottom= AnimationUtils.loadAnimation(this,R.anim.frombottom);
menbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity mainActivity=(MainActivity)getActivity();
mainActivity.loadMysteryFragment();
}
});
i have tried get context now the error it gone but the animation i want does not work
I am assuming that the problem you're talking about happens on this line:
frombottom= AnimationUtils.loadAnimation(this,R.anim.frombottom);
Assuming I'm right about that, the problem is that the first argument to loadAnimation() must be a Context. Activities are Contexts, which is why this works when you do it inside an Activity. However, Fragments are not Contexts, so you can't pass this as the first argument.
Within a Fragment, the easiest way to get a Context object is to call getContext(), though there are parts of the lifecycle where this will return null. Inside onCreateView(), though, this is safe.
So write this instead:
frombottom= AnimationUtils.loadAnimation(getContext(), R.anim.frombottom);

Android Studio Opening an activity

I am trying to open an activity with in an activity. I succeeded to do to from MainActivity but for some reason, this wont allow me to do so again.
I am very new to this, please help me.
package com.example.edonfreiner.siddur;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Davening extends AppCompatActivity {
Button shacharisButton, minchaButton, maarivButton;
public void openShacharis() {
shacharisButton = (Button) findViewById(R.id.shacharis);
shacharisButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent open = new Intent(Davening.this, Shacharis.class);
startActivity(open);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_davening);
openShacharis();
}
}
This is the code to my class, there are no errors, meaning all buttons and references exist in the XML file.
Thank you in advance.
The logic you built is mistaken, cause you are calling a function in your onCreate that will not activate the onClick inside it. And when you click your object, it does not activate the onClick because this is encapsulated inside openShacharis. So the solution is to take off onClick from openShacharis, this way:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_davening);
shacharisButton = (Button) findViewById(R.id.shacharis);
shacharisButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openShacharis();
}
});
}
public void openShacharis() {
Intent open = new Intent(Davening.this, Shacharis.class);
startActivity(open);
}
All activities need to be registered on the Android Manifest.
As you stated on your comment, if you don't have it registered, it won't open.
This documentation will probably help you. In particular the part where it Adds the required <activity> element in AndroidManifest.xml.

How to put some delay in calling an activity after clicking button on an another activity

I am developing a simple app. On one of my activity, there is button which calls the another activity. The another activity starts immediately, but I want to put some delay before opening the next activity after clicking the button.
Handler and Timer methods are not working. I have no idea, What to do???
public class thumb extends ActionBarActivity {
ImageButton show;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thumb);
show=(ImageButton)findViewById(R.id.imageButton);
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent intent=new Intent(v.getContext(),pop.class);
startActivityForResult(intent, 0);
}
});
}
}
Have you tried Thread.sleep(1000);? This will stop the execution by 1000 milliseconds.

Activity switch and Async Android

I have 2 activities: MainActivity which is associated with main.xml layout (app "Home" screen) and AboutActivity which is associated with about.xml layout (app "About" screen).
While in AboutActivity, an Async task within MainActivity still tries to access main.xml. My app to stop working as a result.
Is there any way I can?
"pause" the Async task within MainActivity and "resume" them when
the user switches back from AboutActivity
or still access main.xml in the background while in AboutActivity
Additional information:
MainActivity is the launch activity. AboutActivity extends MainActivity. Users can go to "About" screen/ switch to AboutActivity using the option menu.
The Async task within MainActivity put the user's current location into a textview. about.xml only contains static text. AboutActivity does nothing but show about.xml.
AboutActivity:
public class AboutActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Creating a new non-ui thread task to download Google place json data
PlacesTask placesTask = new PlacesTask();
// Invokes the "doInBackground()" method of the class PlaceTask
placesTask.execute(sb.toString());
}
/** A class, to download Google Places */
private class PlacesTask extends AsyncTask<String, Integer, String>{
String data = null;
// Invoked by execute() method of this object
#Override
protected String doInBackground(String... url) {
//make asynctask wait for debugger
//android.os.Debug.waitForDebugger();
try{
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d(DEBUG,e.toString());
}
return data;
}
// Executed after the complete execution of doInBackground() method
#Override
protected void onPostExecute(String result){
TextView curLoc = (TextView) findViewById(R.id.CurrentLocation);
curLoc.setText(result);
}
}
}
AsyncTask not pause so we can store the data temporary when task complete and while start background activity than show data on textView.
I've solved the problem. It was a partly a variable scope problem and partly due the fact that I cannot use findViewById() there, it always return null
curLoc is null in onPostExecute in the AsyncTask.
I removed the following:
TextView curLoc = (TextView) findViewById(R.id.CurrentLocation);
and declared curLoc as a attribute of the class MainActivity
private TextView curLoc;
and also put in onCreate()
curLoc = (TextView) findViewById(R.id.CurrentLocation);

list view in Android (eclipse)

I made list view with below code and I made array list in values. but I can't see my list when I run my program. What can I do?
public class ListView extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);;
ArrayAdapter<string>adapter;
adapter=new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,com.example.listview.R.array.countries);
setListAdapter(adapter);
}
This line is missing: setContentView(R.layout.simple_list_item_1); , no? Try this.

Resources