I am trying to make a game Tic-Tac-Toe using android studio. When I try to switch from MainActivity to another activity(Game), instead of switching, the button closes the app. I have added several buttons in Game activity but have not used them and android studio is not showing any error about that so is the problem in Game activity or MainActivity? And how to solve this problem.
Main Activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button play = (Button)findViewById(R.id.play);
Button quit = (Button)findViewById(R.id.quit);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openGame();
}
});
quit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
quitGame();
}
});
}
void openGame() {
Intent intent = new Intent(MainActivity.this, Game.class);
startActivity(intent);
}
void quitGame() {
finish();
System.exit(0);
}
}
Game Activity:
public class Game extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ConstraintLayout constraintLayout = (ConstraintLayout)findViewById(R.id.bg);
constraintLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
String color1[] = {"#fb5959", "#fe7738", "#f3980b", "#DBD100", "#A8D700", "#87E50C"};
String color2[] = {"#54F370", "#00FFBC", "#00FFFF", "#00FFE9", "#00C8FF", "#0085FF"};
Random random = new Random();
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BL_TR,
new int[]{Color.parseColor(color1[random.nextInt(6)]),Color.parseColor(color2[random.nextInt(6)])});
constraintLayout.setBackgroundDrawable(gradientDrawable);
return false;
}
});
Button one = (Button) findViewById(R.id.one);
Button two = (Button) findViewById(R.id.two);
Button three = (Button) findViewById(R.id.three);
Button four = (Button) findViewById(R.id.four);
Button five = (Button) findViewById(R.id.five);
Button six = (Button) findViewById(R.id.six);
Button seven = (Button) findViewById(R.id.seven);
Button eight = (Button) findViewById(R.id.eight);
Button nine = (Button) findViewById(R.id.nine);
}
}
Stack Trace:
05/05 13:14:41: Launching 'app' on OnePlus ONE A2003.
$ adb shell am start -n "com.example.tictactoe/com.example.tictactoe.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 21369 on device 'oneplus-one_a2003-3222ee10'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Choreographer: Skipped 107 frames! The application may be doing too much work on its main thread.
E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()
D/AppTracker: App Event: start
E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()
D/AppTracker: App Event: stop
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tictactoe, PID: 21369
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tictactoe/com.example.tictactoe.Game}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.tictactoe.Game.onCreate(Game.java:20)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class Button
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.tictactoe.Game.onCreate(Game.java:20)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x10101e6 a=-1}
at android.content.res.TypedArray.getDrawable(TypedArray.java:867)
at android.view.View.<init>(View.java:3964)
at android.widget.TextView.<init>(TextView.java:680)
at android.widget.Button.<init>(Button.java:109)
at android.widget.Button.<init>(Button.java:105)
at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:72)
at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:68)
at androidx.appcompat.app.AppCompatViewInflater.createButton(AppCompatViewInflater.java:192)
at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:111)
at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.tictactoe.Game.onCreate(Game.java:20)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
D/AppTracker: App Event: crash
I/Process: Sending signal. PID: 21369 SIG: 9
Game Activity Xml Code:
<?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:id="#+id/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0D95DF"
tools:context=".Game">
<Button
android:id="#+id/one"
android:layout_width="91dp"
android:layout_height="90dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.015"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.029" />
<Button
android:id="#+id/two"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.034" />
<Button
android:id="#+id/three"
android:layout_width="91dp"
android:layout_height="90dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.984"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.034" />
<Button
android:id="#+id/four"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.015"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.497" />
<Button
android:id="#+id/five"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.497" />
<Button
android:id="#+id/six"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.984"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.497" />
<Button
android:id="#+id/seven"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.015"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.975" />
<Button
android:id="#+id/eight"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.975" />
<Button
android:id="#+id/nine"
android:layout_width="91dp"
android:layout_height="92dp"
android:background="?android:attr/selectable"
android:text="\?"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.984"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.975" />
<TextView
android:id="#+id/player1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player 1 (X) :"
android:textColor="#000000"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.177"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.098" />
<EditText
android:id="#+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintStart_toEndOf="#+id/player1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.088" />
<TextView
android:id="#+id/player2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player 2 (O) :"
android:textColor="#000000"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.175"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.177" />
<EditText
android:id="#+id/name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintStart_toEndOf="#+id/player1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="20dp"
app:layout_constraintGuide_percent="0.3" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="462dp"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp"
app:layout_constraintGuide_percent="0.15" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="358dp"
app:layout_constraintGuide_percent="0.85" />
<View
android:id="#+id/vertical1"
android:layout_width="4dp"
android:layout_height="282dp"
android:background="?android:attr/listDivider"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.5"
tools:visibility="visible" />
<View
android:id="#+id/vertical2"
android:layout_width="4dp"
android:layout_height="282dp"
android:background="?android:attr/listDivider"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.5"
tools:visibility="visible" />
<View
android:id="#+id/horizontal1"
android:layout_width="282dp"
android:layout_height="4dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.333" />
<View
android:id="#+id/horizontal2"
android:layout_width="282dp"
android:layout_height="4dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.666" />
</androidx.constraintlayout.widget.ConstraintLayout>
what you have is an inflation exception, Android is unable to inflate Button because it does not recognize an ?attr/... i had a similar problem a while back. Try removing this line in xml.
android:background="?android:attr/selectable"
if problem persists remove all lines with.
android:.......="?attr/...."
Related
I am so frustrated that i probably wont be able to explain well, but basically im trying to code a music app player however whenever i click the button the app just crashes. The Logcat doesnt really help much since i dont know whats wrong with it. Please help me out T.T
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.startButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Proceeding to login page", Toast.LENGTH_LONG).show();
openLogIn();
}
});
}
public void openLogIn(){
Intent intent = new Intent(this, Login.class);
startActivity(intent);
}
}
activity_main.xml:
<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="#color/black"
tools:context=".MainActivity">
<TextView
android:id="#+id/musicAppTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="112dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="112dp"
android:text="#string/apollotunes"
android:textColor="#color/white"
android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginTop="35dp"
android:layout_marginEnd="57dp"
android:gravity="center"
android:text="#string/millions_of_songs_to_choose_from_n_with_no_interruptions"
android:textColor="#color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_marginStart="55dp"
android:layout_marginTop="49dp"
android:layout_marginEnd="56dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/musicAppTitle"
app:srcCompat="#drawable/project_logo" />
<Button
android:id="#+id/startButton"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="81dp"
android:text="#string/let_s_get_started"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textStart" />
Login.java:
"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
TextView email = (TextView) findViewById(R.id.email);
TextView password = (TextView) findViewById(R.id.password);
Button loginbtn = (Button) findViewById(R.id.logInButton);
loginbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(email.getText().toString().equals("flanders070105#gmail.com") && password.getText().toString().equals("2202231C"))
{
Toast.makeText(Login.this, "", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(Login.this, "LOGIN FAILED", Toast.LENGTH_SHORT).show();
}
});
}
}"
activity_login.xml:
"
<TextView
android:id="#+id/signInText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="83dp"
android:layout_marginTop="76dp"
android:layout_marginEnd="84dp"
android:text="Sign into your account!"
android:textColor="#color/white"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/email"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="31dp"
android:backgroundTint="#color/white"
android:drawableLeft="#drawable/ic_baseline_email_24"
android:drawablePadding="15dp"
android:hint="Email"
android:textColorHint="#color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/signInText" />
<EditText
android:id="#+id/password"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="31dp"
android:backgroundTint="#color/white"
android:drawableLeft="#drawable/ic_baseline_lock_24"
android:drawablePadding="15dp"
android:hint="Password"
android:textColorHint="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email"
tools:textSize="20sp" />
<Button
android:id="#+id/logInButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="153dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="164dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/password" />
error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.finalapollotunes/com.example.finalapollotunes.Login}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.finalapollotunes.Login.onCreate(Login.java:16)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.content.res.Resources$NotFoundException: Drawable (missing name) with resource ID #0x7f0501a5
Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f0501a5
at android.content.res.ResourcesImpl.getResourceName(ResourcesImpl.java:255)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:785)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:631)
at android.content.res.Resources.loadDrawable(Resources.java:897)
at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:955)
at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
enter cE/AndroidRuntime: at android.view.View.<init>(View.java:5010)
at android.view.ViewGroup.<init>(ViewGroup.java:659)
at android.view.ViewGroup.<init>(ViewGroup.java:655)
at android.view.ViewGroup.<init>(ViewGroup.java:651)
at androidx.constraintlayout.widget.ConstraintLayout.<init>(ConstraintLayout.java:587)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.finalapollotunes.Login.onCreate(Login.java:16)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)ode here
this how your activity_login.xml should look like, you have to inclose the layout tag
<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="#color/black"
tools:context=".LoginActivity">
<TextView
android:id="#+id/signInText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="83dp"
android:layout_marginTop="76dp"
android:layout_marginEnd="84dp"
android:text="Sign into your account!"
android:textColor="#color/white"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/email"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="31dp"
android:backgroundTint="#color/white"
android:drawableLeft="#drawable/ic_baseline_email_24"
android:drawablePadding="15dp"
android:hint="Email"
android:textColorHint="#color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/signInText" />
<EditText
android:id="#+id/password"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="31dp"
android:backgroundTint="#color/white"
android:drawableLeft="#drawable/ic_baseline_lock_24"
android:drawablePadding="15dp"
android:hint="Password"
android:textColorHint="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email"
tools:textSize="20sp" />
<Button
android:id="#+id/logInButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="153dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="164dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/password" />
</androidx.constraintlayout.widget.ConstraintLayout>
Android terminal
I getting errors in homepage of my new app of helpline,I tried to check all but still not running please check and comment accordingly Please review
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.googlemaps, PID: 18785
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemaps/com.example.googlemaps.HomeActivity}: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7078)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)
Caused by: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class fragment
Caused by: java.lang.NullPointerException
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Fragment.instantiate(Fragment.java:524)
at android.app.FragmentContainer.instantiate(FragmentContainer.java:53)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3551)
at android.app.FragmentController.onCreateView(FragmentController.java:103)
at android.app.Activity.onCreateView(Activity.java:6445)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:819)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:902)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:863)
at android.view.LayoutInflater.inflate(LayoutInflater.java:554)
at android.view.LayoutInflater.inflate(LayoutInflater.java:461)
at android.view.LayoutInflater.inflate(LayoutInflater.java:383)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.googlemaps.HomeActivity.onCreate(HomeActivity.java:24)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7078)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)
xml file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context="HomeActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="125dp"
android:layout_marginTop="5dp"
android:text="Welcome! You are logged in"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText"
android:layout_marginStart="125dp" />
<TextView
android:id="#+id/texttitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="125dp"
android:layout_marginTop="25dp"
android:text="24x7 SECURITY HELPLINE"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText"
android:layout_marginStart="50dp" />
<fragment
android:id="#+id/home"
android:layout_height="match_parent"
android:layout_width="140dp"
android:layout_marginTop="80dp"
tools:context=".HomeActivity"
android:background="#FA8072" />
<ImageButton
android:id="#+id/imageButton"
android:layout_width="140dp"
android:layout_height="130dp"
android:layout_marginTop="80dp"
tools:src="#tools:sample/avatars"
tools:ignore="HardcodedText"
android:contentDescription="Profile Pic"/>
<Button
android:id="#+id/profile"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="210dp"
android:text="Your Profile"
tools:ignore="HardcodedText"/>
<Button
android:id="#+id/complain"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:text="Complain"
tools:ignore="HardcodedText"/>
<Button
android:id="#+id/tips"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="290dp"
android:text="Safety Tips"
tools:ignore="HardcodedText"/>
<Button
android:id="#+id/alerts"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="330dp"
android:text="Alerts"
tools:ignore="HardcodedText"/>
<Button
android:id="#+id/emergency"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="370dp"
android:text="emergency"
tools:ignore="HardcodedText"/>
<Button
android:id="#+id/nearby"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="410dp"
android:text="NearBy"
tools:ignore="HardcodedText"/>
<Button
android:id="#+id/volunteer"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="450dp"
android:text="Volunteer"
tools:ignore="HardcodedText"/>
<Button
android:id="#+id/logout"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginTop="490dp"
android:text="Logout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
tools:ignore="HardcodedText"
android:layout_marginStart="1dp" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="170dp"
android:layout_marginLeft="170dp"
android:layout_marginTop="400dp"
android:shadowRadius="3"
android:textColor="#0000FF"
android:text="Location sharing"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="150dp"
android:background="#drawable/custom_button"
android:layout_marginStart="150dp" />
<Switch
android:id="#+id/switch_enable_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginLeft="200dp"
android:layout_marginTop="350dp"
android:text="Panic Alert"
android:layout_marginStart="200dp"
tools:ignore="HardcodedText"/>
<TextView
android:id="#+id/textView3"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="225dp"
android:layout_marginLeft="210dp"
android:text="Panic Mode"
android:textColor="#color/colorPrimaryDark"
android:textSize="20sp"
tools:ignore="HardcodedText"
android:layout_marginStart="200dp" />
<Button
android:id="#+id/report"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="480dp"
android:layout_marginLeft="170dp"
android:text="Report"
android:textColor="#FF0000"
tools:ignore="HardcodedText"
android:layout_marginStart="180dp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:layout_marginLeft="150dp"
tools:ignore="HardcodedText"
android:text="Create your panic alert button "
android:layout_marginStart="150dp" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="650dp"
android:layout_marginLeft="150dp"
tools:ignore="HardcodedText"
android:text="Feedback"
android:textColor="#FFA500"
android:layout_marginStart="150dp" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="650dp"
android:layout_marginLeft="280dp"
tools:ignore="HardcodedText"
android:text="Write to us"
android:textColor="#FFA500"
android:layout_marginStart="280dp" />
</FrameLayout>
java file
package com.example.googlemaps;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
public class HomeActivity extends AppCompatActivity {
Button btnLogout;
Button customButton;
Switch switchEnableButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
launchactivity();
}
});
btnLogout = findViewById(R.id.logout);
customButton = findViewById(R.id.custom_button);
switchEnableButton = findViewById(R.id.switch_enable_button);
customButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(HomeActivity.this, "Click", Toast.LENGTH_SHORT).show();
}
});
switchEnableButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
customButton.setEnabled(true);
} else {
customButton.setEnabled(false);
}
}
});
btnLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Intent intToMain = new Intent(HomeActivity.this, LoginActivity.class);
startActivity(intToMain);
}
});
}
public void launchactivity(){
Intent intent = new Intent( HomeActivity.this, MapsActivity.class );
startActivity( intent );
}
}
I getting errors in homepage of my new app of helpline,I tried to check all but still not running please check and comment accordingly
Please review
Your code to define a fragment in xml is incorrectly. Pls read this: https://developer.android.com/training/basics/fragments/creating.
Hope this help.
You forgot about android:name="" attribute in your fragment. If you want to add your fragment dynamically, please use ViewGroup (FrameLayout, etc.)
i've seen some answers suggest that i should change to this line of code
android.support.v7.widget.GridLayout
or
androidx.gridlayout.widget.GridLayout
none work.
also importing as v7 and as androidx didnt work.
please help. it worked fine until now :(
thank you!
MainActivity:
package com.example.braindtrainer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
GridLayout myGridLayout;
Button goButton;
TextView timeTextView;
TextView scoreTextView;
TextView questionTextView;
TextView notifyTextView;
public void goClick(View view){
Log.i("goButton","Pressed");
timeTextView.setVisibility(View.VISIBLE);
scoreTextView.setVisibility(View.VISIBLE);
questionTextView.setVisibility(View.VISIBLE);
notifyTextView.setVisibility(View.VISIBLE);
goButton.setVisibility(View.INVISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myGridLayout = findViewById(R.id.myGridLayout);
goButton = findViewById(R.id.goButton);
timeTextView = findViewById(R.id.timerTextView);
scoreTextView = findViewById(R.id.scoreTextView);
questionTextView = findViewById(R.id.questionTextView);
notifyTextView = findViewById(R.id.notifyTextView);
}
}
Layout
<?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"
tools:context=".MainActivity">
<Button
android:id="#+id/goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#B388FF"
android:onClick="goClick"
android:text="GO!"
android:textSize="70dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.gridlayout.widget.GridLayout
android:id="#+id/myGridLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="300dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="300dp"
android:visibility="invisible"
app:columnCount="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount="2">
<TextView
android:id="#+id/firstTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#991230C8"
android:tag="1"
android:text="567"
android:textColor="#000000"
android:textSize="100sp"
app:layout_column="0"
app:layout_row="0" />
<TextView
android:id="#+id/secondTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A9C62424"
android:tag="2"
android:text="567"
android:textColor="#000000"
android:textSize="100sp"
app:layout_column="1"
app:layout_row="0" />
<TextView
android:id="#+id/thirdTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9A60E841"
android:tag="3"
android:text="567"
android:textColor="#000000"
android:textSize="100sp"
app:layout_column="0"
app:layout_row="1" />
<TextView
android:id="#+id/fourthTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#94CFBF30"
android:tag="4"
android:text="567"
android:textColor="#000000"
android:textSize="100sp"
app:layout_column="1"
app:layout_row="1" />
</androidx.gridlayout.widget.GridLayout>
<TextView
android:id="#+id/timerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#EA80FC"
android:text="15 s"
android:textColor="#000000"
android:textSize="60sp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/scoreTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="#FFAB40"
android:text="0/0"
android:textColor="#000000"
android:textSize="60dp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/questionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="29dp"
android:layout_marginLeft="29dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="34dp"
android:layout_marginRight="34dp"
android:background="#3E3C1515"
android:text="4 + 6"
android:textColor="#000000"
android:textSize="50dp"
android:visibility="invisible"
app:layout_constraintEnd_toStartOf="#+id/scoreTextView"
app:layout_constraintStart_toEndOf="#+id/timerTextView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/notifyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="39dp"
android:text="00000"
android:textColor="#000000"
android:textSize="50dp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/myGridLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
Log
2020-03-22 18:22:13.472 7929-7929/com.example.braindtrainer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.braindtrainer, PID: 7929
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.braindtrainer/com.example.braindtrainer.MainActivity}: java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout cannot be cast to android.widget.GridLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout cannot be cast to android.widget.GridLayout
at com.example.braindtrainer.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
From the crash log is says there is a class cast exception. You used the GridLayout from the androidx library in the xml whereas, you are trying to cast it to the GridLayout from the android library modules in your java class. Hence you are getting this exception. You need to use the same version for both of your layout xml and the java class.
You need to import the following in your Java class.
import androidx.gridlayout.widget.GridLayout
And remove the following
import android.widget.GridLayout;
I've been working on an app (which i took over from another student's final year project on my lecturer's recommendation) due to university internship requirements and am having issues with migrating from the main menu screen to the login screen.
(Only the login page has issues, going from the application's main menu to the register page has no issues whatsoever, hence i assume the problem might be either in the xml files or the LoginActivity.java, but have no idea how.)
logcat errors are listed as such:
TAL EXCEPTION: main
Process: com.finchvpn.androidcloudpark, PID: 1579
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.finchvpn.androidcloudpark.MainActivity.loginButtonClick(MainActivity.java:74)
Below are related xml files:
Main menu xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/asd2">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar3"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/side_nav_bar"
app:popupTheme="#style/PopupOverlay" />
<TextView
android:id="#+id/toolbar_title"
android:layout_width="239dp"
android:layout_height="73dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:text="CloudPark.my"
android:textColor="#color/md_white_1000"
android:textSize="14pt"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.11"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.014" />
<ImageButton
android:id="#+id/loginButtonPic"
android:layout_width="132dp"
android:layout_height="120dp"
android:layout_gravity="end"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:background="#null"
android:onClick="loginButtonClick"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.19"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.559"
app:srcCompat="#drawable/loginnew2"
tools:ignore="ContentDescription" />
<ImageButton
android:id="#+id/RegisterButtonPic"
android:layout_width="132dp"
android:layout_height="120dp"
android:layout_gravity="end"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:background="#null"
android:onClick="registerButtonClick"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.813"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.559"
app:srcCompat="#drawable/registernew1"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/textView2"
android:layout_width="279dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="CloudPark, the next generation parking app."
android:textColor="#color/md_white_1000"
android:textSize="10pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.181" />
</android.support.constraint.ConstraintLayout>
Login xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/asd2">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar3"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/side_nav_bar"
app:popupTheme="#style/PopupOverlay" />
<TextView
android:id="#+id/toolbar_title"
android:layout_width="307dp"
android:layout_height="91dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Login"
android:textColor="#color/md_white_1000"
android:textSize="14pt"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView2"
android:layout_width="281dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Enter your credentials to access CloudPark."
android:textColor="#color/md_white_1000"
android:textSize="10pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.181" />
<EditText
android:id="#+id/textUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Username"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.352" />
<EditText
android:id="#+id/textPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.443" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#0026FF"
android:onClick="loginButtonClick"
android:text="Login to CloudPark"
android:textColor="#FFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.687" />
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
</android.support.constraint.ConstraintLayout>
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
private EditText textUsername;
private EditText txtPassword;
private static RestClient restClient = new RestClient();
private SharedPreferences.Editor sharedPreferencesEditor;
#SuppressLint("CommitPrefEdits")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
try {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
} catch (Exception e) {
}
textUsername = findViewById(R.id.textUsername);
txtPassword = findViewById(R.id.textPassword);
SharedPreferences sharedPreferences = getSharedPreferences("UserInfo", 0);
sharedPreferencesEditor = sharedPreferences.edit();
textUsername.setText(sharedPreferences.getString("textUsername", ""));
txtPassword.setText(sharedPreferences.getString("txtPassword", ""));
}
public static RestClient getRestClient() {
return restClient;
}
public void loginButtonClick(View v) {
if (!textUsername.getText().toString().equals("") && !txtPassword.getText().toString().equals("")) {
apiPostLogin(Constants.ANDROID_KEY + ":" + textUsername.getText().toString() + ":" + txtPassword.getText().toString());
sharedPreferencesEditor.putString("textUsername", textUsername.getText().toString());
sharedPreferencesEditor.putString("txtPassword", txtPassword.getText().toString());
sharedPreferencesEditor.commit();
} else {
Toast.makeText(LoginActivity.this, "NULL", Toast.LENGTH_LONG).show();
}
}
private void apiPostLogin(String data) {
final ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Logging in");
progress.setMessage("Please wait ...");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
Call<ResponseBody> call = getRestClient().getLoginService().postLogin(data);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful() && response.body() != null) {
try {
String data = response.body().string();
JSONObject jsonObject = new JSONObject(data);
Constants.uid = Integer.parseInt(jsonObject.getString("id"));
Constants.username = jsonObject.getString("username");
Constants.email = jsonObject.getString("email");
Constants.credit = jsonObject.getString("credit");
Constants.qr_code = jsonObject.getString("qr_code");
Constants.created_at = jsonObject.getString("created_at");
Constants.updated_at = jsonObject.getString("updated_at");
Toast.makeText(LoginActivity.this, "apiPostLogin onResponse <<<< \r\n\r\n" + jsonObject.toString(), Toast.LENGTH_LONG).show();
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
progress.dismiss();
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(LoginActivity.this, "Incorrect username/password, please try again." + t.getMessage(), Toast.LENGTH_LONG).show();
progress.dismiss();
}
});
}
}
I suspect the problem is related with the clash of android:onClick="loginButtonClick" in main menu xml and login xml.
You should not depend on android:onClick attribute for handling View click. It is because when using android:onClick you can't be sure that your method handling the onClick will work. There is no exact mechanism to ensure your code is connected to the View. Another problem is, android:onClick won't work for Fragment. So, I consider using android:onClick as bad practice.
To solve the problem, use setOnClickListener on your View. Coupled with findViewById, your code will more robust because you will always see an error if you are giving an incorrect id for findViewById. To make your code more robust and avoid clashing the id, you need to use a decscriptive name id for the view. Use naming convention like this:
layout name + _ + What the view for + _ + type of view
For example, for your login xml you can use something like this:
....
<Button
android:id="#+id/login_cloudpark_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#0026FF"
android:text="Login to CloudPark"
android:textColor="#FFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.687" />
<android.support.constraint.Guideline
android:id="#+id/login_begin_gdl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
...
then use it with findViewById:
public class LoginActivity extends AppCompatActivity {
...
private Button mBtnLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
...
mBtnLogin = findViewById(R.id.login_cloudpark_btn);
}
}
after that, add clickListener to mBtnLogin:
mbtnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// place your clicking handle code here.
}
});
By doing the above, you will separate the View in xml and your logic cleanly.
I have two activities Main, and Game_Activity
When my app launches and I click a button to take me to the Game_Activity it crashes...
Main
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHelp = (Button) findViewById(R.id.btnHelp);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnSettings = (Button) findViewById(R.id.btnSettings);
btnStart.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, GameActivity.class); //gets us to Game activity when Start Button is clicked
startActivity(i);
overridePendingTransition(R.animator.animation1, R.animator.animation2);
}
});
}
Game_Activity
ViewGroup kamilsLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kamilsLayout = (ViewGroup) findViewById(R.id.kamilsLayout);
Button kamilsButton2 = (Button) findViewById(R.id.kamilsButton2);
kamilsButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
moveButton();
}
});
}
public void moveButton(){
View kamilsButton = findViewById(R.id.kamilsButton);
TransitionManager.beginDelayedTransition(kamilsLayout);
//Change size of the button
ViewGroup.LayoutParams sizeRules = kamilsButton.getLayoutParams();
sizeRules.height = 700;
kamilsButton.setLayoutParams(sizeRules);
}
Here is Logcat sorry for last lines but couldn't format it... :/
03-02 21:32:49.073 22427-22427/com.example.kamil.mychemicalreaction E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.kamil.mychemicalreaction, PID: 22427
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kamil.mychemicalreaction/com.example.kamil.mychemicalreaction.GameActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.kamil.mychemicalreaction.GameActivity.onCreate(GameActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)at android.app.ActivityThread.access$900(ActivityThread.java:161)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:126)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:157)at android.app.ActivityThread.main(ActivityThread.java:5356)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)at dalvik.system.NativeStart.main(Native Method)
XML
Game_Activity
<Button
android:layout_width="150dp"
android:layout_height="1dp"
android:id="#+id/kamilsButton"
android:gravity="bottom|fill_vertical"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#ff6aff01" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Click"
android:id="#+id/kamilsButton2"
android:textSize="9dp"
android:clickable="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="102dp" />
Menu
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Chemical Reaction"
android:id="#+id/txtTitle"
android:textStyle="bold"
android:textColor="#ffa946ff"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btnStart"
android:text="Start"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_marginBottom="61dp"
android:layout_above="#+id/btnSettings"
android:layout_alignLeft="#+id/btnSettings"
android:layout_alignStart="#+id/btnSettings" />
<Button
android:id="#+id/btnSettings"
android:text="Settings"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_above="#+id/btnHelp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp" />
<Button
android:id="#+id/btnHelp"
android:text="Help"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/btnSettings"
android:layout_alignStart="#+id/btnSettings"
android:layout_marginBottom="81dp" />
You're getting a NullPointerException when trying to reference your UI elements. From your XML, it appears you haven't given any layout an id of kamilsLayout. Try enclosing your UI elements in a layout with this ID.
Like Josef said. You're setting the same XML for two both activities, is that expected?
You're using setContentView(R.layout.activity_main). Maybe there is no ViewGroup on R.layout.activity_main layout. And you're trying to setting up that `
kamilsLayout = (ViewGroup) findViewById(R.id.kamilsLayout);
`