how I process NullPointerException on android? [duplicate] - java

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);
}

Related

Android studio creating new intent

I find myself trying to create a new intent to use it for switching activities in my android app using the android studio IDE.
However, I get an error saying cannot resolve constructor when I try to do so.
Here is what my code looks like.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, Menu.class);
}
});
}
}
Where Menu.class is another java class empty activity
It looks like you have imported wrong menu class. Just import the one you made and it will fix this error.
How I came to this conclusion ?
Android has Menu class inbuilt , so you must have imported that class. which will throw this error.
It is a good practice to add Activity after the name of activities. for example : MenuActivity
You could call postDelayed(#NonNull Runnable r, long delayMillis) method instead. Like below:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getBaseContext(), Menu.class);
startActivity(intent);
}
},0);
if you use explicit intent, make your code like this.
Intent i = new Intent(getApplicationContext(), Menu.class);
startActivity(i);
You can also make intent in single line,
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getBaseContext(), Menu.class));
}
},1000);

Starting new activity from recyclerview adapter

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));

How to pass variable from onClick to another class [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 7 years ago.
I was trying to pass a variable named choice to another class but I do not know how I should go along doing this because it is inside the onClick. Here is an example of what I am trying to do.
public int choice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//intent for first button
Button button1= (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
choice = 1;
startActivity(new Intent(MainActivity.this,Celeb1.class));
}
});
//intent for second button
Button button2= (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
choice = 2;
startActivity(new Intent(MainActivity.this,Celeb1.class));
}
});
}
In a different class I would like to check if choice is equal to 1 or 0 and execute some code.
You can pass variable by this way
Intent intent = new Intent(MainActivity.this,Celeb1.class)
intent.putExtra("choice", 1);
startActivity(intent);
And in Celeb1 class you can get variable by using
Bundle extras = getIntent().getExtras();
int choice= extras.getInt("choice");
inside onCreate()
//in main activity
Intent i=new Intent(getApplicationContext(),Celeb1.class);
i.putExtra("name","stack");//name is key stack is value
startActivity(i);
//In Celeb1 class
Intent i=getIntent();
String name=i.getStringExtra("name");
Do not make instance of Intent instead make object of Intent and pass extra values to your class
choice = 2;
Intent intent = new Intent(MainActivity.this, Celeb1.class);
intent.putExtra("choice",choice);
startActivity(intent);
That's It
I am android beginner, Hope It will help you.

How to create 2 or more than 2 button in Android

I want to create 2 or more than 2 two button in android but I got the problem in this line
View btnClick = findViewById(R.id.buttonClick);
View btnscan = findViewById(R.id.btscanClick);
//set event listener
btnClick.setOnClickListener(this);
}
//override the OnClickListener interface method
#Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.buttonClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
public void onClick1(View arg1) {
if(arg1.getId() == R.id.btscanClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,ScanActivity.class);
//start the second Activity
this.startActivity(intent);
may be the problem would be with your design, I thing you are implementing it in surface view. if you do so make your layout with LinearLayout and RelativeLayout, it would be better if you also add your .manifestfile
Hey cast your views to Button i.e. instead of
View btnscan = findViewById(R.id.btscanClick);
use
Button btnscan = (Button)findViewById(R.id.btscanClick);
also post the errors you are getting so that I can help you out.
Add listener to other button as well
Button btnscan = (Button)findViewById(R.id.btscanClick);
btnscan.setOnClickListener(this);
Button btnclick = (Button)findViewById(R.id.buttonClick);
btnclick.setOnClickListener(this);
And let handle the click with only one OnClick method.
public void onClick(View arg0) {
if(arg0.getId() == R.id.buttonClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
}
else if(arg1.getId() == R.id.btscanClick){
//define a new Intent for the second Activity
Intent intent = new Intent(this,ScanActivity.class);
//start the second Activity
this.startActivity(intent);
}
}
The way you usually want to go to define the OnClickListener for buttons is as follow :
btnClick.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
}
});
btnScan.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
//define a new Intent for the scan Activity
Intent intent = new Intent(this,ScanActivity.class);
//start the second Activity
this.startActivity(intent);
}
});
This means that you use a different onClickListener for each button.

How to navigate from one screen to another screen

How to navigate from one Activity screen to another Activity screen? In the first screen I'm having one button if I click the button it has to move to another Activity screen.
The most trivial case (called from activity):
startActivity(new Intent(this, ActivityToLaunch.class));
More details here: http://developer.android.com/guide/topics/fundamentals.html
OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(action));
}
};
Button button = (Button) findViewById(id);
button.setOnClickListener(onClickListener);
Button x.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent i = new Intent(y.this, Activity.class);
startActivity(i);
}
});
Here we've defined a listener for Button x. The OS will call this method and start the Activity referenced in Intent i.
Here's the official tutorial example:
http://developer.android.com/guide/tutorials/notepad/notepad-ex2.html
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(TestActivity.this,second.class));
}
});
public void onClick(View v)
{
Intent myintent = new Intent(currentclass.this, nextactivity.class);
startActivity(myintent);
}
This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html
public void startActivity (Intent intent) -- Used to launch a new activity.
So suppose you have two Activity class and on a button click's OnClickListener() you wanna move from one Activity to another then --
PresentActivity -- This is your current activity from which you want to go the second activity.
NextActivity -- This is your next Activity on which you want to move.
So the Intent would be like this
Intent(PresentActivity.this, NextActivity.class)
Finally this will be the complete code
public class PresentActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);
// currentContext.startActivity(activityChangeIntent);
PresentActivity.this.startActivity(activityChangeIntent);
}
});
}
}
This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener() at any place where you want to switch between your activities.
final Context cont = this;
Button btnClickABC =(Button)findViewById(R.id.btnClickABC);
btnClickABC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(cont, NextActivity.class));
}
});
Use following code..I hope this will help you.
Button button = (Button)findViewById(R.id.xxx);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
startActivity(intent);
}
});
xxx is id from your xml of your Button.
startActivity(new Intent(this,newActivity.class));
Switching from one activity to another is really simple, but tricky for a new one.
Your next class must be defined in AndroidManifest.xml. This is tester class:
<activity
android:name=".Tester"
android:label="#string/title_activity_tester" >`enter code here`
</activity>
final Button button = (Button) findViewById(R.id.btnGo);// btnGo is id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(CurrentClass.this, Tester.class);
startActivity(i);
}
You can navigate to the next screen using these code snippets:
Kotlin
startActivity(Intent(this, LoginActivity::class.java))
Java
startActivity(new Intent(this, LoginActivity.class))
Here's a reference: Android Developers - Starting another activity
Intent intentobj=new Intent(FromActivity.this,ToActivity.class);
startActivity(intentobj);
or you can simply use
startActivity(new Intent(FromActivity.this,ToActivity.class));
For Kotlin (if you are in an activity)
buttonToClick.setOnClickListener{ startActivity(this,YourDestinationActivity::class.java)
}
If you are in a fragment
buttonToClick.setOnClickListener{
startActivity(requireActivity, YourDestinationActivity::class.java)
}
In your method fun onCreate(savedInstanceState: Bundle?) add this.
your_btn_id.setOnClickListener{
val intent = Intent(this, yourpagename::class.java)
startActivity(intent)
}
Until now if it doesn't work then, check if these two files are added or not,
import android.content.Intent
import kotlinx.android.synthetic.main.activity_otp.*
Try this code:
Button my_btn;
my_btn = findViewById(R.id.submit_btn);
my_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_2);
}
});
Button navigate;
navigate = findViewById(R.id.button);
navigate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Navigate another Activity",Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
});
Just go to XML file and add Onclick = "opennewactivity" in button xml.
Then go to java code and create class opennewactivity you can just make it my clciking alt+Enter in xml code's "opennewactivity". in that just write
Intent intent = new Intent(this, newacivity.class);
startActivity(intent);
and if you want user to not get back to first activity again then just write this
Intent intent = new Intent(this, newactivity.class);
startActivity(intent);
finish();
Cartoon_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
newActivity();
}
});
}
public void newActivity()
{
Intent selectClass= new Intent(getApplicationContext(), com.example.fyp.videoplayer.class);
startActivity(selectClass);
}

Categories

Resources