i keep getting thrown a NullPointer Exception in Android 4.1 while switching between Activities. Any thoughts on why?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
Button login = (Button)findViewById(R.id.login_button);
Button join = (Button)findViewById(R.id.join_button);
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(LaunchActivity.this, LoginActivity.class);
startActivity(i);
}});//END OF CLICK
join.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(LaunchActivity.this, JoinActivity.class);
startActivity(i);
}});//END OF CLICK
}
From your question description i am assuming that you are getting this error because, you haven't add LoginActivity & JoinActivity both these activity into your AndroidManifest.xml. Just add them into AndroidManifest.xml as follows,
<activity android:name=".LoginActivity"></activity>
<activity android:name=".JoinActivity"></activity>
Related
I'm trying to start another activity by pressing on the cardview which has a friend finder id. But when I write home.java it gives me problems in the setOnClickListener. At homeActivity it tells me Cannot resolve method 'homeActivity' in 'HomeActivity'. because?
public class HomeActivity extends AppCompatActivity {
private CardView btn_home;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_home = findViewById(androidx.appcompat.R.id.home);
btn_home.setOnClickListener(v -> homeActivity(new Intent(HomeActivity.this, TrovamicoActivity.class)));
}
btn_home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, TrovamicoActivity.class);
startActivity(intent);
}
});
If there is no code in the manifest, write it
<activity android:name=".TrovamicoActivity" />
i try to make an intent which is used to go to a 2nd activity. however when i try to click on it, it makes my application crash.
here's the code:
public class MainActivity extends AppCompatActivity {
private Button mButtonCommande;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonCommande=findViewById(R.id.buttonAccessCommande);
mButtonCommande.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openCommandeActivity();
}
});
}
public void openCommandeActivity() {//cette méthode me sert à envoyer vers l'activité commande
Intent intent = new Intent(this, CommandeActivity.class );
startActivity(intent);
}
idk where it keeps crashing and i need help.
Make sure you declared the target activity in AndroidManifest.xml.
In your AndroidManifest.xml, add the following inside the <application> element:
<activity android:name=".CommandeActivity"/>
for example i have made a Nodemcu server and there are 3 links on the main webpage(192.168.18.100).
the thing i want to try is that when i click an android button this link get accessed without opening any new activity i.e i want to stay on the main activity.
this is my code so far that works but not according to what i want it.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button on = (Button) findViewById(R.id.button);
Button off =(Button) findViewById(R.id.button2);
Button pink = (Button) findViewById(R.id.button3);
on.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16236607"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_SEND, uri);
startActivity(intent);
}
});
off.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16203967"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
pink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16214167"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
You can try to implement a WebView on your MainActivity. When you click on any of your button, load the WebView with your desired url.
You can find the way to implement WebView in this link
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSighin = (Button) findViewById(R.id.btnSignIn);
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent j = new Intent(MainActivity.this, SignUp.class);
startActivity(SignUp);
}
});
btnSighin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent k = new Intent(MainActivity.this, SignIn.class);
startActivity(SignIn);
}
});
}
I am working on an app that allows customers to book appointments and in order to do so, they need to sign up or sign in. I have activities made for both of them and I can't run it without getting the "Expression Expected" and I'm new to Android Studio and have no idea what to do. Any help?
You are missing some information. Anyway, i see that you have mistakes in lines
startActivity(SignUp);
and
startActivity(SignIn);
The correct way should be startActivity(j) and startActivity(k) respectively
You have to pass the intent object to the start activity method not the signup or signin class
So it should be startActivity(i) and startActivity(j)
Its been a while since I made an app. So I may be forgetting something. Whenever I press a button to start another intent, the app crashes. Is there something I'm missing?
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button playButton = findViewById(R.id.play_button);
playButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent play = new Intent(MainActivity.this, PointsActivity.class);
startActivity(play);
}
});
Button leaderButton = findViewById(R.id.leader_board);
leaderButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent leader = new Intent(MainActivity.this, LeaderBoard.class);
startActivity(leader);
}
});
}
}
Look like you are not declaring your next activity in AndroidMainfest.xml
<manifest ... >
<application ... >
<activity android:name=".PointsActivity" />
<activity android:name=".LeaderBoard" />
...
</application ... >
...
</manifest >
Without your stacktrace it is impossible to know what your error is, are you getting invalid casting exceptions? Usually you need to cast widgets into objects before assigning them to variables Ie your line
Button playButton = findViewById(R.id.playbutton);
should be
Button playButton = (Button)findViewById(R.id.playbutton);
notice the explicit cast to Button, you may be getting errors like cannot cast android.widget.button to button, this will fix those.