I am reading data from firebase real time database to a recyclerview and on the recyclerview item there a button to start a new activity. I have this onClick method
public void bookBtnOnClick(final int position){
mBookButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mContext.startActivity(new Intent(this,ReservationForm.class));
}
});
}
But it cannot resolve intent constructer
try-->
Intent intent = new Intent(context, ReservationForm.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(intent);
instead of this try mContext as i guess you are trying it inside Adapter class
mContext.startActivity(new Intent(mContext,ReservationForm.class));
Change 'this' to 'mContext';
mContext.startActivity(new Intent(mContext,ReservationForm.class));
Adding more to #S.ambika's answer.
Change this to mContext when creating Intent.
mContext.startActivity(new Intent(mContext,ReservationForm.class));
As when you use this, it will give the current context. In your case this is returning the context of onClick and for starting a new Activity, we need the context of Activity.
try this:
public void bookBtnOnClick(final int position){
mContext.startActivity(new Intent(context, ReservationForm.class));
}
How's about this
Intent intent = new Intent (v.getContext(), ReservationForm.class);
v.getContext().startActivity(intent);
Define Context mContext; on starting of adapter and in constructor use this.mcontext=mcontext;
mContext.startActivity(new Intent(mContext,ReservationForm.class));
Related
I am new to Android development and I am trying to start another activity when the user clicks on an item in a ListView. However, I am stuck trying to create an Intent to do it. I am trying to follow this guide, where they use the this keyword. However, it doesn't work in this case because this refers to the AdapterView.onItemClickListener.
My current code is this:
list.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter,
View item, int pos, long id) {
Intent intent = new Intent(this, OtherActivity.class);
// use the Intent to start another activity...
}
});
What should I be replacing this with?
use your activity name before this, example:
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
The code worked back when I still used an EditText, but I changed it to an AutoCompleteTextView to make things easier for the user, and now I have a problem. Here is the bulk of the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);
String[] items = getResources().getStringArray(R.array.items_array);
java.util.Arrays.sort(items);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
edit.setAdapter(adapter);
public void find(View view) {
String name = edit.getText().toString();
if(name.equalsIgnoreCase("Brown Turkey Fig")){
Intent intent = new Intent(this, BrownTurkeyFig.class);
startActivity(intent);
}
else if(name.equalsIgnoreCase("Emerald Green Arborvitae")){
Intent intent = new Intent(this, EmeraldGreenArborvitae.class);
startActivity(intent);
}
else if(name.equalsIgnoreCase("Delaware Valley White Azalea")){
Intent intent = new Intent(this, DelawareValleyWhiteAzalea.class);
startActivity(intent);
}
Ive included the parts that are related to the question. Basically, the user types the name of a plant in the AutoCompleteTextView, and then clicks a button to be brought to a new activity based on whatever plant they typed in (I included three as examples at the end). Before I added the autofill part, the code worked fine the way it was.
The problem is that when the button is clicked, the next activity is not brought up. It crashes the app.
I do not know what is wrong with it now. Perhaps I am not properly taking the actual text from the AutoCompleteTextView to be compared in my if statements? Any help is appreciated!
You are declaring a local variable "edit" in your oncreate method but also you are using a global variable in your "find" method.
I'm sorry , I can't understand what's your meaning. Maybe you can try to use AutoCompleteTextView.setOnItemClickListener. For example:
edit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = items.get(position);
if(item.equalsIgnoreCase("Brown Turkey Fig")){
Intent intent = new Intent(this, BrownTurkeyFig.class);
startActivity(intent);
} else if(item.equalsIgnoreCase("Emerald GreenArborvitae"){
Intent intent = new
Intent(this,EmeraldGreenArborvitae.class);
startActivity(intent);
} else if(item.equalsIgnoreCase("Delaware Valley White Azalea")){
Intent intent = new Intent(this, DelawareValleyWhiteAzalea.class);
startActivity(intent);
}
}
})
Hope to help you.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I want
when I click the button.
showing Connectingreceiver.class
but NullpointerException.
I think I not well use context.
advice for me
thanks android senior developer.
private OnClickListener mConnectOnClick = new OnClickListener() {
Context context= ConfiguredNetworkContent.this;
#Override
public void onClick(View v) {
Intent intent = new Intent(context,Connectingreceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
public class Connectingreceiver extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connecting_dialog);
}
}
Check this :
private View.OnClickListener mConnectOnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Connectingreceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(intent);
}
Update :
Straight forward explanation is you need a Context to start a Activity. And in Onclick() there is a passing View in which Context already existed. So, I only used it to start activity.
where this context? Context context;
fix Context context = getApplicationContext;
Try this
// Add this line after Main class
Button yourButton;
In below code don't forget to edit "yourButtonIDfromXML"
// Add below code in OnCreate method after setContentView
Button yourButton = (Button) findViewById(R.id.yourButtonIDfromXML);
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ConfiguredNetworkContent.this, Connectingreceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
});
The "context" - is null. You need to do null check s to avoid Null Pointer Crash. It is the easiest exception to resolve.
If your are doing OnClick event in
Activity:- Use these lines.
public static void startReceiver() {
Intent intent = new Intent(YourActivityName.this, Connectingreceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Fragment :-
Use these lines
private OnClickListener mConnectOnClick = new OnClickListener() {
Context context;
#Override
public void onClick(View v) {
if(getActivity() != null) {
startActivity(getActivity());
}
}
And use same the startActivity() method
Also, rename your startActivity method. It is an inbuilt android method.
Prefer using camel cases for your class name. Connectingreceiver --> ConnectingReceiver.
I think I not well use context.
You have to use it if needed, But as per your codes there is no initialization of context.
First initialize your context either with ApplicationContext or ActivityContext like here
Context context = YourActivity.this;
then you can use startActivity(intent);. You don't need to write context.startActivity(intent);. only startActivity will be enough at all.
UPDATE :
Do not pass any context their just simply do
customstartActivity();
And inside customstartActivity() method
public static void customstartActivity (){
// Intent intent = new Intent(yourActivity.this, Connectingreceiver.class);
// Intent intent = new Intent(ContextWrapper.getBaseContext(), Connectingreceiver.class);
Intent intent = new Intent(getApplicationContext(), Connectingreceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
I have a Relative layout (useMe) that loads one of two fragments on creation depending on the orientation of the screen. I keep getting the error
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.RelativeLayout.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
the problem is this little bit of code right here
RelativeLayout useMe = (RelativeLayout) findViewById(R.id.startpage_activity);
useMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(StartPage.this, Question1.class);
startActivity(intent);
}
});
How is the useMe a null object?
Are you Trying to start another Activity?, or Another Fragment?
btw, you can use this set-up of Intent:
1st: include android:OnClick= "useMe" and android:Clickable="true" in your Relative layout and do this one:
public void useMe(View view)
{
Intent intent = new Intent(getActivity(), Question1.class);
startActivity(intent);
finish();
}
hope it helps :D
P.S: if OnClick in RelativeLayout is not working, try it on LinearLayout, it is working 100% just include the Onclick and Clickable thingy in in your code :)
Intent intent = new Intent(getActivity(), Question1.class);
startActivity(intent);
and define Activity in Manifest file
I have the following code:
chart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final String aux= (String) lt.getItemAtPosition(position);
Intent myIntent = new Intent(infoList.this, tabList.class);
startActivity(myIntent);
}
});
I also have a ListView. When I click on an item from that ListView I navigate to another activity that shows me the info for that activity. How do I do that? I want that info to correspond to the item I clicked.
Here is what I did in this situation, if you don't want to just pass an id back:
I call the other activity with this:
Intent intent = new Intent(myapp, CreateExerciseActivity.class);
intent.putExtra("selected_id", workout.getId());
startActivityForResult(intent, CREATE_ACTIVITY);
I then do
Intent returnIntent = new Intent();
returnIntent.putExtra("name", m.getName());
returnIntent.putExtra("unit", m.getUnit());
returnIntent.putExtra("quantity", m.getQuantity());
if (getParent() == null) {
setResult(Activity.RESULT_OK, returnIntent);
} else {
getParent().setResult(Activity.RESULT_OK, returnIntent);
}
finish();
So, in this case I was passing in an id in order to get more details from the user, and then I pass those back to the calling activity, in order to add it, as I didn't want to save this until the user chooses to later.
So, you need to do startActivityForResult in order to have it able to return the data.
You can add some data to the Intent with the methods putExtra(), and then retrieve the data in the new Activity with getExtras().getSomething().
i guess you have to use OnItemClickListener instead of the click event and you need intent to call the next activity.
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Intent intent = new Intent(this, MyInfoActivity.class);
intent.putExtra("selected_id", getIdFor(position));
startActivity(intent);
}
};
mHistoryView = (ListView)findViewById(R.id.history);
mHistoryView.setOnItemClickListener(mMessageClickedHandler);
Possible answer: How do I pass data between Activities in Android application?
http://thedevelopersinfo.wordpress.com/2009/10/15/passing-data-between-activities-in-android/
It really depends on what the data is in your listview. For example, if you're displaying a list of contacts in the listview, you could just pass the ID of the contact over to the other activity, and let that activity access the content provider for contacts to retrieve the data you want it to work with. You'd pass an ID within a URI in the data field of the intent, as opposed to in the extras bundle.