I am new to Android Studio and have been pulling my hair out for the past hour trying to figure this out.
So I have a MainActivity java class which has two separate onClick methods each with an intent that opens a certain activity depending on the button (in activity_main.mxml) pressed.
For whatever reason, the newUser.setOnClickListener() will not open SignupActivity.
The strangest part is that if I change the destination activity in that same block to SplashActivity, it works. This tells me that it's a problem with my SignupActivity and its corresponding.xml file maybe.
Any help is greatly appreciated.
Here are the files in my project:
MainActvitiy.java:
package com.example.neurow;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
// Declare buttons
Button existingUser, newUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Hide Action bar and Status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
// Define buttons
existingUser = findViewById(R.id.btnExistingUser);
newUser = findViewById(R.id.btnNewUser);
// Existing user button listener
existingUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SignupActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
// New user button listener
newUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SignupActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
}
}
SignupActivity.java:
package com.example.neurow;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
public class SignupActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide Action bar and Status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_login);
}
// Launch MainActivity when back button is pressed
public void launchMain (View v) {
// Launch Log-in activity
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
LoginActivity.java:
package com.example.neurow;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide Action bar and Status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_login);
}
// Launch MainActivity when back button is pressed
public void launchMain (View v) {
// Launch Log-in activity
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
activity_signup.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_background"
tools:context=".SignupActivity">
<!-- Welcome Back Text -->
<TextView
android:id="#+id/txtWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="330dp"
android:fontFamily="sans-serif-light"
android:text="Create User"
android:textColor="#color/white"
android:textSize="60sp"
android:textStyle="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- User ID Field -->
<EditText
android:id="#+id/edtTxtPromptUserID"
android:layout_width="332dp"
android:layout_height="69dp"
android:layout_marginTop="468dp"
android:backgroundTint="#color/white"
android:hint="#string/prompt_userID"
android:inputType="text"
android:selectAllOnFocus="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Password Field -->
<EditText
android:id="#+id/edtTxtPromptPassword"
android:layout_width="332dp"
android:layout_height="69dp"
android:layout_marginTop="8dp"
android:backgroundTint="#color/white"
android:hint="#string/prompt_password"
android:inputType="textPassword"
android:selectAllOnFocus="true"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/edtTxtPromptUserID" />
<!-- Register Button -->
<Button
android:id="#+id/btnRegister"
android:layout_width="242dp"
android:layout_height="83dp"
android:layout_marginTop="40dp"
android:backgroundTint="#00A36C"
android:text="Register"
android:textSize="30sp"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/edtTxtPromptPassword" />
<!-- Back button -->
<Button
android:id="#+id/btnBack2"
android:layout_width="134dp"
android:layout_height="57dp"
android:layout_marginTop="24dp"
android:onClick="launchMain"
android:text="Back"
android:textColor="#color/white"
android:textSize="20dp"
android:backgroundTint="#color/purple_200"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnRegister" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have gone back and looked at all my files to make sure they are unique and consistent with naming, but I haven't been able to find the reason why it won't work.
change in signup Activity
setContentView(R.layout.activity_login);
to
setContentView(R.layout.activity_signup);
In SignupActivity > onCreate()
just replace yours with following...
setContentView(R.layout.activity_signup);
you are binding wrong xml with your java file.
'setContentView()' is used to bind layouts (xml)
Related
Hello everybody I got an issue when I trying to run my app in my physical phone and just get crashed at run. its my 5 time making this app, when this happen just close at installing the app. I'm trying to make an app in the main activity two Image buttons and then open anther activity, each one, I checked myself without creating the image buttons. but when I putted the image button the app just get crashed.
here is my code.
ActivityMain.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/blurred"
tools:context=".MainActivity">
<ImageView
android:id="#+id/blurbackground"
android:layout_width="match_parent"
android:layout_height="97dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/backmain" />
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="18dp"
android:layout_marginLeft="18dp"
android:layout_marginTop="62dp"
android:text="Welcome!"
android:textColor="#android:color/white"
android:textSize="22sp" />
<TextView
android:id="#+id/txt2"
android:layout_width="345dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="7dp"
android:layout_marginTop="114dp"
android:gravity="center"
android:text="Master all professions in the professions guide"
android:textColor="#android:color/white"
android:textColorHighlight="#android:color/black"
android:textSize="18sp"
android:typeface="sans" />
<TextView
android:id="#+id/txt4"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_marginBottom="348dp"
android:textColorHighlight="#android:color/black"
android:text="Be part of the game and be the master of professions, these guides are collected from users who offer great help in your adventure throughout Azeroth!"
android:textColor="#android:color/white"
android:textStyle="italic"
android:typeface="sans" />
<TextView
android:id="#+id/txt7"
android:layout_width="322dp"
android:layout_height="64dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginBottom="75dp"
android:text="We have classroom guides available for you to master your character if you are fresh and new to the world of Warcraft, dominate your enemies and take the victory!"
android:textColor="#android:color/white"
android:textColorHighlight="#android:color/black" />
<ImageView
android:id="#+id/imageView"
android:layout_width="227dp"
android:layout_height="103dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="66dp"
android:layout_marginLeft="66dp"
android:layout_marginTop="223dp"
app:srcCompat="#drawable/alchem"
tools:scaleType="centerCrop" />
<ImageButton
android:id="#+id/buthorde"
android:layout_width="68dp"
android:layout_height="75dp"
android:layout_alignStart="#+id/imageView"
android:layout_alignParentBottom="true"
android:layout_marginStart="3dp"
android:layout_marginBottom="155dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/horde" />
<ImageButton
android:id="#+id/butally"
android:layout_width="70dp"
android:layout_height="73dp"
android:layout_alignEnd="#+id/imageView"
android:layout_alignBottom="#+id/buthorde"
android:layout_marginEnd="5dp"
android:layout_marginBottom="3dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/ally" />
<Button
android:id="#+id/butshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="21dp"
android:text="Show me!"
android:textAllCaps="false"
android:textStyle="italic"
android:typeface="sans" />
ActivityMain.java
package hordemuzzle.wowprofessions;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button butonhorde;
private Button butonally;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--------------------------------------------------------------------------- -------------------------
TextView textView = (TextView) findViewById(R.id.txt1);
textView.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView.setText("Welcome!");
TextView textView1 = (TextView) findViewById(R.id.txt2);
textView1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView1.setText("Master all professions in the professions guide");
//----------------------------------------------------------------------------------------------------
butonally=(Button) findViewById(R.id.butally);
butonally.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActally();
}
});
butonhorde=(Button) findViewById(R.id.buthorde);
butonhorde.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActhorde();
}
});
}
public void openActhorde(){
Intent intenth = new Intent(this, Activityhorde.class);
startActivity(intenth);
}
public void openActally(){
Intent intenta = new Intent(this, Activityally.class);
startActivity(intenta);
}
}
Logcat
2018-11-01 23:30:05.204 1051-19358/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
2018-11-01 23:30:05.204 1051-19358/system_process D/OpenGLRenderer: Swap behavior 1
2018-11-01 23:30:05.291 1051-12094/system_process D/GraphicsStats: Buffer count: 4
2018-11-01 23:30:05.291 1051-4361/system_process I/WindowManager: WIN DEATH: Window{3382c8d u0 hordemuzzle.wowprofessions/hordemuzzle.wowprofessions.SplashScreenActivity}
2018-11-01 23:30:05.291 1051-12088/system_process I/ActivityManager: Process hordemuzzle.wowprofessions (pid 24362) has died
2018-11-01 23:30:05.291 1051-12088/system_process D/ActivityManager: cleanUpApplicationRecord -- 24362
Splash screen activity
package hordemuzzle.wowprofessions;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import gr.net.maroulis.library.EasySplashScreen;
import static android.support.v4.os.LocaleListCompat.create;
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EasySplashScreen config = new EasySplashScreen(SplashScreenActivity.this)
.withFullScreen()
.withTargetActivity(MainActivity.class)
.withSplashTimeOut(4000)
.withBackgroundResource(android.R.color.background_dark)
.withLogo(R.drawable.logo);
View easysplashscreen = config.create();
setContentView(easysplashscreen);
}
}
try this
package hordemuzzle.wowprofessions;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--------------------------------------------------------------------------- -------------------------
TextView textView = (TextView) findViewById(R.id.txt1);
textView.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView.setText("Welcome!");
TextView textView1 = (TextView) findViewById(R.id.txt2);
textView1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView1.setText("Master all professions in the professions guide");
//----------------------------------------------------------------------------------------------------
((ImageButton) findViewById(R.id.butally)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActally();
}
});
((ImageButton) findViewById(R.id.buthorde)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActhorde();
}
});
}
public void openActhorde(){
Intent intenth = new Intent(this, Activityhorde.class);
startActivity(intenth);
}
public void openActally(){
Intent intenta = new Intent(this, Activityally.class);
startActivity(intenta);
}
}
I am trying to use this Firebase authentication in my project, and I am getting an error that i can't figure out how to solve, and what causes it:
My xml of the layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="info.androidhive.firebase.LoginActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimary"
android:gravity="center"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:layout_width="#dimen/logo_w_h"
android:layout_height="#dimen/logo_w_h"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:src="#mipmap/ic_launcher" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="#string/hint_email"
android:inputType="textEmailAddress"
android:textColor="#android:color/white"
android:textColorHint="#android:color/white" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="#string/hint_password"
android:inputType="textPassword"
android:textColor="#android:color/white"
android:textColorHint="#android:color/white" />
</android.support.design.widget.TextInputLayout>
<!-- Login Button -->
<Button
android:id="#+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#color/colorAccent"
android:text="#string/btn_login"
android:textColor="#android:color/black" />
<Button
android:id="#+id/btn_reset_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#null"
android:text="#string/btn_forgot_password"
android:textAllCaps="false"
android:textColor="#color/colorAccent" />
<!-- Link to Login Screen -->
<Button
android:id="#+id/btn_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#null"
android:text="#string/btn_link_to_register"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="15dp" />
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone" />
My code
package com.esmad.pdm.friendlymanager;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class LoginActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Button btnSignup, btnLogin, btnReset;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnSignup = (Button) findViewById(R.id.btn_signup);
btnLogin = (Button) findViewById(R.id.btn_login);
btnReset = (Button) findViewById(R.id.btn_reset_password);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, SignupActivity.class));
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//authenticate user
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
// there was an error
if (password.length() < 6) {
inputPassword.setError(getString(R.string.minimum_password));
} else {
Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
}
} else {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
});
}
});
}
}
Why I keep getting this error, I'm using ConstraintLayouts to build this application, what am I missing?
Your CoordinatorLayout has the id 'toolbar'
<android.support.design.widget.CoordinatorLayout
..
android:id="#+id/toolbar"
..>
But in Java you cast it to a Toolbar object
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
This throws the exception because the View associated with the id R.id.toolbar is not a Toolbar.
To fix this, remove the above two lines from your Java code, since you do not actually have a Toolbar in your layout.
You should probably also change the id for the CoordinatorLayout to something more descriptive such as "#+id/coordinator"
Edit: If I look at the example you are recreating, there is no Toolbar there.
And honestly for a login page it wouldn't make the most sense. However if you want to add it you could add this to your layout. Place it as the first child of the top LinearLayout:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
But if you are not really confident with handling layouts and such, I would highly advice to read some more documentations and try simpler examples to get famillar with the platform.
A good place to start is here: https://developer.android.com/guide/topics/ui/declaring-layout.html
check your import modules carefully you should import this module
android.support.v7.widget.Toolbar
instead of this one
android.widget.Toolbar
hello I'm new to android and I have a problem with multiple button first I implemented multiple button in my MainActivity and it is work with me now I create another activity and implement the same what i do in the mainActivity in the new activity I have a multiple button in this activity
the new Activity (Activity6.class):
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Activity6 extends AppCompatActivity implements
View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adminbackdoor);
Button ee = (Button) findViewById(R.id.button12);
Button ff = (Button) findViewById(R.id.button9);
Button gg = (Button) findViewById(R.id.button10);
Button hh = (Button) findViewById(R.id.button11);
ee.setOnClickListener(this);
ff.setOnClickListener(this);
gg.setOnClickListener(this);
hh.setOnClickListener(this);
}
#Override
public void onClick (View vv){
switch (vv.getId()) {
case R.id.button12:
Intent e = new Intent(Activity6.this, Admin1.class);
startActivity(e);
break;
case R.id.button9:
Intent f = new Intent(Activity6.this, User1.class);
startActivity(f);
break;
case R.id.button10:
Intent g = new Intent(Activity6.this, Teacher1.class);
startActivity(g);
break;
case R.id.button11:
Intent h = new Intent(Activity6.this, Class1.class);
startActivity(h);
break;
default:
break;
}
}
Admin1.class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Admin1 extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin);
}
}
User1.class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class User1 extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adminuser);
}
}
activity Teacher1.class and Class1.class have the same with Admin1.class and User1.class but with different layout.
Note: I add all the activities in manifest like this:
<activity android:name=".Activity6"></activity>
<activity android:name=".Admin1"></activity>
<activity android:name=".Class1"></activity>
<activity android:name=".Teacher1"></activity>
<activity android:name=".User1"></activity>
adminbackdoor.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="العمليات"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:fontFamily="serif"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginTop="60dp"
android:layout_marginRight="140dp" />
<Button
android:id="#+id/button12"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="admin"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="#+id/button9"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="user"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="#+id/button10"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="teacher"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="#+id/button11"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="class"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
and onClick does not work with me I do not know what is the problem ??
if I run the application and click on the button they did not work ( go to another layout )
First of all you should extend Activity and not AppCompatActivity.
And one thing I can recommend is to use the click listener like this in this situation:
Button ee = (Button) findViewById(R.id.button12);
ee.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent e = new Intent(Activity6.this, Admin1.class);
startActivity(e);
}
});
I have a table row which contains textviews
I am trying to use intents to click on the text view to move to the next activity but when I click on the text view I get the following error
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'tv_msisdn'
at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4479)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4443)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
Code Hear
package com.tela.mobile.home;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.tela.mobile.R;
public class DeviceAlertDetail extends Activity implements OnClickListener
{
private TextView tv_number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablerow_overage);
TextView textView = (TextView)findViewById(R.id.tv_number);
tv_number.setOnClickListener(this);
}
public void onClick(View view)
{
Intent intent = new Intent(this, SearchResultsActivity.class);
this.startActivity(intent);
}
}
and I am setting onClick for the textView in my layout file as follows
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:padding="1dp"
android:text="number"
android:id="#+id/tv_number"
android:textSize="12sp"
android:maxLines="1"
android:onClick="onClick"
android:clickable="true"
android:ellipsize="end"
/>
You don't have to implement OnClickListener when you use android:onClick="onClick" just let public void onClick(View view) as it and remove implements OnClickListener from the activity
Your code gonna be like
package com.tela.mobile.home;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.tela.mobile.R;
public class DeviceAlertDetail extends Activity
{
private TextView tv_number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablerow_overage);
}
public void onClick(View view)
{
Intent intent = new Intent(this, SearchResultsActivity.class);
this.startActivity(intent);
}
}
and
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:padding="1dp"
android:text="number"
android:id="#+id/tv_number"
android:textSize="12sp"
android:maxLines="1"
android:onClick="onClick"
android:clickable="true"
android:ellipsize="end"
/>
I think you didn't initialize correctly tv_number.
You have created an unused local variable TextView textView, which may have confused you in thinking that you initialized tv_number.
Please replace
TextView textView = (TextView)findViewById(R.id.tv_number);
With
tv_number = (TextView)findViewById(R.id.tv_number);
I think you are misplace reference name.
Just change
tv_number.setOnClickListener(this);
with
textView.setOnClickListener(this);
Your Code Become
//Activity
package com.tela.mobile.home;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.tela.mobile.R;
public class DeviceAlertDetail extends Activity implements OnClickListener
{
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablerow_overage);
textView = (TextView)findViewById(R.id.tv_number);
textView.setOnClickListener(this);
}
public void onClick(View view)
{
Toast.makeText(this,"Code Working Fine Now Remove Comments Of Below",Toast.LENGTH_LONG).show();
//Intent intent = new Intent(this, SearchResultsActivity.class);
//this.startActivity(intent);
}
}
//.xml becomes
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:padding="1dp"
android:text="number"
android:id="#+id/tv_number"
android:textSize="12sp"
android:maxLines="1"
android:clickable="true"
android:ellipsize="end"
/>
Hello Stack Overflow !
I'm working on an android application for children : Each activity is a question.
For each question, there is two buttons for clue and the answer.
I encounter two issues :
1/ I have to click on a button before change activity, do I need an override or something so I can change of activity without any click required ?
2/ On this activity (code is below), if I click on the button with the id "essai", the activity stop and I came to the previous activity.. Anybody know how I could fix this ?
Thank you in advance !
Here is the java code
package com.beryl.timewaster;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class Activity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
}
public void indice1(View v)
{
Toast.makeText(this, "expression", Toast.LENGTH_LONG).show();
}
public void indice2(View v)
{
Toast.makeText(this, "aguéri", Toast.LENGTH_LONG).show();
}
public void essai(View v)
{
Toast.makeText(this, "LOLILO", Toast.LENGTH_LONG).show();
final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche1);
ib2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Activity1.this, Accueil.class);
startActivity(intent);
}
});
}}
And here the xml
<AbsoluteLayout
android:id="#+id/absoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxx"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:layout_x="-1dp"
android:layout_y="61dp"
android:text="#string/dev2"
android:textColor="#color/blanc"
android:textSize="25sp"
android:color="#color/blanc" />
<Button
android:id="#+id/Solution2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="259dp"
android:onClick="essai"
android:text="#string/solution"
android:textColor="#color/vertpomme"
android:textSize="30sp" />
<Button
android:id="#+id/indice1de2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="1dp"
android:layout_y="121dp"
android:onClick="indice1"
android:text="#string/indice1"
android:textColor="#color/orange"
android:textSize="30sp" />
<Button
android:id="#+id/indice2de2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="190dp"
android:onClick="indice2"
android:text="#string/indice2"
android:textColor="#color/rouge"
android:textSize="30sp" />>
<ImageButton
android:id="#+id/flechedroite2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="196dp"
android:layout_y="338dp"
android:src="#drawable/flechedroite" />
<ImageButton
android:id="#+id/flechegauche2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="33dp"
android:layout_y="339dp"
android:src="#drawable/flechegauche" />
</AbsoluteLayout>
</RelativeLayout>
Change final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche1); to final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche2);. You have not declared android:id="#+id/flechegauche1" in your xml