Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
My Error message:
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.blood_doner, PID: 9696
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blood_doner/com.example.blood_doner.MainActivity}: android.view.InflateException: Binary XML file line #11 in com.example.blood_doner:layout/activity_main: Binary XML file line #11 in com.example.blood_doner:layout/activity_main: Error inflating class LinearLayoutCompat
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.view.InflateException: Binary XML file line #11 in com.example.blood_doner:layout/activity_main: Binary XML file line #11 in com.example.blood_doner:layout/activity_main: Error inflating class LinearLayoutCompat
Caused by: android.view.InflateException: Binary XML file line #11 in com.example.blood_doner:layout/activity_main: Error inflating class LinearLayoutCompat
Caused by: java.lang.ClassNotFoundException: android.view.LinearLayoutCompat
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at android.view.LayoutInflater.createView(LayoutInflater.java:813)
at android.view.LayoutInflater.createView(LayoutInflater.java:774)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:911)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:928)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:948)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1002)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.blood_doner.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.LinearLayoutCompat" on path: DexPathList[[zip file "/data/app/~~E_zA6DvG2UIaGgWdJTrtLw==/com.example.blood_doner-WiH1_nFAcKgFOSBhTUwffw==/base.apk"],nativeLibraryDirectories=[/data/app/~~E_zA6DvG2UIaGgWdJTrtLw==/com.example.blood_doner-WiH1_nFAcKgFOSBhTUwffw==/lib/x86, /system/lib, /system_ext/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 33 more
I/Process: Sending signal. PID: 9696 SIG: 9
My activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/background_image"
android:id="#+id/drawer_layout"
tools:context=".MainActivity">
<LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
layout="#layout/main_toolbar"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home"
android:textSize="50sp"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayoutCompat>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:gravity="start"
android:background="#drawable/background_image">
</RelativeLayout>
<include
layout="#layout/main_nav_drawer"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:id="#+id/top_layout"
>
<ImageView
android:id="#+id/helpline_button"
android:layout_width="180dp"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:background="#drawable/rounded_image"
android:padding="5dp"
android:layout_below="#+id/request_button"
app:srcCompat="#drawable/helpline"
tools:ignore="MissingConstraints"
/>
<ImageView
android:id="#+id/request_button"
android:layout_width="180dp"
android:layout_height="150dp"
android:layout_marginRight="20dp"
android:layout_marginTop="50dp"
android:background="#drawable/rounded_image"
android:padding="5dp"
app:srcCompat="#drawable/request"
android:layout_alignParentLeft="true"
/>
<ImageView
android:id="#+id/organization_button"
android:layout_width="180dp"
android:layout_height="150dp"
android:layout_alignParentRight="true"
android:layout_marginTop="50dp"
android:background="#drawable/rounded_image"
android:padding="5dp"
app:srcCompat="#drawable/organization" />
<ImageView
android:id="#+id/plasma_button"
android:layout_width="180dp"
android:layout_height="150dp"
android:background="#drawable/rounded_image"
android:padding="5dp"
android:layout_marginTop="10dp"
app:srcCompat="#drawable/plasma"
android:layout_below="#+id/organization_button"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="30dp"
android:layout_below="#+id/top_layout"
android:id="#+id/blood_group"
>
<ImageView
android:id="#+id/a_positive_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
app:srcCompat="#drawable/a_positive" />
<ImageView
android:id="#+id/a_negative_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
android:layout_toRightOf="#+id/a_positive_button"
app:srcCompat="#drawable/a_negative"
tools:layout_editor_absoluteY="420dp" />
<ImageView
android:id="#+id/b_positive_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
android:layout_toRightOf="#+id/a_negative_button"
app:srcCompat="#drawable/b_positive" />
<ImageView
android:id="#+id/b_negative_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/rounded_image"
android:layout_toRightOf="#+id/b_positive_button"
app:srcCompat="#drawable/b_negative"
/>
<ImageView
android:id="#+id/ab_positive_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
android:layout_below="#+id/a_positive_button"
app:srcCompat="#drawable/ab_positive" />
<ImageView
android:id="#+id/ab_negative_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
android:layout_below="#+id/a_negative_button"
android:layout_toRightOf="#+id/ab_positive_button"
app:srcCompat="#drawable/ab_negative" />
<ImageView
android:id="#+id/o_positive_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
android:layout_below="#+id/a_negative_button"
android:layout_toRightOf="#+id/ab_negative_button"
app:srcCompat="#drawable/o_positive" />
<ImageView
android:id="#+id/o_negative_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:background="#drawable/rounded_image"
android:layout_toRightOf="#+id/o_positive_button"
android:layout_below="#+id/b_negative_button"
app:srcCompat="#drawable/o_negative" />
</RelativeLayout>
<Button
android:id="#+id/log_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:onClick="logout"
android:text="Log out"
android:layout_below="#+id/blood_group"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
/>
</RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>
My main Activity.java
package com.example.blood_doner;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
//Initialize variable
DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawer_layout);
}
public void ClickMenu(View view){
//open drawer
openDrawer(drawerLayout);
}
public static void openDrawer(DrawerLayout drawerLayout) {
//open drawer layout
drawerLayout.openDrawer(GravityCompat.START);
}
public void ClickLogo(View view){
closeDrawer(drawerLayout);
}
public static void closeDrawer(DrawerLayout drawerLayout) {
//close drawer layout
//checks condition
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
//when drawer is open close the drawer
drawerLayout.closeDrawer(GravityCompat.START);
}
}
public void ClickHome(View view){
//recreate activity
recreate();
}
public void ClickDashboard(View view){
//redirect activity to dashboard
redirectActivity(this, Dashboard.class); //Dashboard.class
}
public void ClickAboutUs(View view){
redirectActivity(this, AboutUs.class); //AboutUs.class
}
public void ClickLogout(View view){
Logout(this);
}
//temporary
public static void Logout(Activity activity) {
// AlertDialog.Builder = new AlertDialog.Builder(activity);
// builder.setTitle("Logout");
// builder.setMessage("Are you sure you want to logout?");
// builder.setPositiveButton("YES", new DialogInterface.OnClickListener(){
// #Override
// public void onClick(DialogInterface dialog, int which){
// activity.finishAffinity();
System.exit(0);
// }
// });
// builder.setNegativeButton("NO", new DialogInterface.OnClickListener(){
// #Override
// public void onClick(DialogInterface dialog, int which){
// dialog.dismiss();
//
// }
//
// });
// builder.show();
}
public static void redirectActivity(Activity activity, Class aClass) {
Intent intent = new Intent(activity, aClass);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
#Override
protected void onPause() {
super.onPause();
closeDrawer(drawerLayout);
}
public void logout(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(), Login.class));
finish();
}
}
I didn't understand what's the issue is? I'am new to android development and I don't understand any of the error message. Please help me fix this problem! The application is crashing when I'm trying to run! I have given my code sample and error message on the top!
Replace
<LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
with
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
i'm getting java.lang.NullPointerException. And i don't know how to solve it. looking for some help here
What i'v tried:
to look here java.lang.RuntimeException: Unable to start activity ComponentInfo But, it didn't help me
to clean and Rebuild project.
to Invalidate Cache and Restart in Android studio
and i'v discovered some problems in LayoutInflater.java
It cant resolve some imports"
sorry cant import images yet this is and image of errors in LayoutInflater.java
My XML file activity_main.XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="Team A" />
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="24dp"
android:gravity="center"
android:textSize="50sp"
android:text="0" />
<Button
android:id="#+id/three_points_button_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusThreePointsA"
android:text="+3 Points" />
<Button
android:id="#+id/two_points_button_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusTwoPointsA"
android:text="+2 Points" />
<Button
android:id="#+id/free_throw_button_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusOnePointA"
android:text="Free Throw" />
</LinearLayout>
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="Team B" />
<TextView
android:id="#+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="24dp"
android:gravity="center"
android:textSize="50sp"
android:text="0" />
<Button
android:id="#+id/three_points_button_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusThreePointsB"
android:text="+3 Points" />
<Button
android:id="#+id/two_points_button_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusTwoPointsB"
android:text="+2 Points" />
<Button
android:id="#+id/free_throw_button_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusOnePointB"
android:text="Free Throw" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center"
android:orientation="vertical">
<Button
android:id="#+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="24dp"
android:onClick="reset"
android:text="Reset" />
</LinearLayout>
</LinearLayout>
My MainActivity.java
package com.example.problems;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
int scoreA = 0;
int scoreB = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void plusThreePointsA(View view){
scoreA = scoreA+3;
displayScoreA(scoreA);
}
public void plusTwoPointsA(View view){
scoreA = scoreA+2;
displayScoreA(scoreA);
}
public void plusOnePointA(View view){
scoreA = scoreA+1;
displayScoreA(scoreA);
}
public void plusThreePointsB(View view){
scoreB = scoreB+3;
displayScoreB(scoreB);
}
public void plusTwoPointsB(View view){
scoreB = scoreB+2;
displayScoreB(scoreB);
}
public void plusOnePointB(View view){
scoreB = scoreB+1;
displayScoreB(scoreB);
}
public void displayScoreA (int score){
TextView viewScore = findViewById(R.id.team_a_score);
viewScore.setText(String.valueOf(score));
}
public void displayScoreB (int score){
TextView viewScore = findViewById(R.id.team_b_score);
viewScore.setText(String.valueOf(score));
}
public void reset (View view){
scoreA = 0;
scoreB = 0;
displayScoreA(scoreA);
displayScoreB(scoreB);
}
}
Error that i get
--------- beginning of crash
05-11 08:14:25.720 4084-4084/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.problems, PID: 4084
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.problems/com.example.problems.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:715)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.problems.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-11 08:19:26.000 4084-4084/com.example.problems I/Process: Sending signal. PID: 4084 SIG: 9
Use :
<View android:layout_width="1dp" android:layout_height="match_parent" android:background="#android:color/darker_gray" />
instead of :
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
/>
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I'm creating an app for a sensor and when I start it the app crashes.
I tried the app on 2 different phones and 2 emulators but crashes un every device.
Here is the MainActivity.java file:::
package com.example.ir_sensor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openMenu = findViewById(R.id.Open_menu);
closeMenu = findViewById(R.id.Close_menu);
}
View openMenu = findViewById(R.id.Open_menu);
View closeMenu = findViewById(R.id.Close_menu);
public void menu() {
if (openMenu.getVisibility() == View.VISIBLE) {
closeMenu.setVisibility(View.GONE);
} else if (closeMenu.getVisibility() == View.GONE){
openMenu.setVisibility(View.VISIBLE);
}
}
}
Here is the activity_main.xml file:::
<?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">
<ImageView
android:id="#+id/LOGO"
android:layout_width="0dp"
android:layout_height="248dp"
android:layout_marginStart="8dp"
android:layout_marginTop="52dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:contentDescription="#string/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo_int_lukka" />
<ImageView
android:id="#+id/Close_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="49dp"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/LOGO"
app:srcCompat="#drawable/ic_menu_less" />
<ImageView
android:id="#+id/Open_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="49dp"
android:contentDescription="#string/openmenu"
android:visibility="visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/LOGO"
app:srcCompat="#android:drawable/ic_menu_more" />
<LinearLayout
android:id="#+id/menuLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="380dp"
android:baselineAligned="false"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/houseMenu"
android:layout_width="4dp"
android:layout_height="70dp"
android:layout_weight="1"
android:contentDescription="#string/housemenu"
app:srcCompat="#drawable/house_menu" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/IR_LED"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:contentDescription="#string/ir_led"
app:srcCompat="#drawable/ir_led" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/alarmSign"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:contentDescription="#string/alarmsign"
app:srcCompat="#drawable/on_off__alert" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
</FrameLayout>
</LinearLayout>
</LinearLayout>
<androidx.constraintlayout.widget.Guideline
android:id="#+id/gridline380FromTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="380dp"
android:orientation="horizontal"
app:layout_constraintGuide_begin="380dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/gridline48FromLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:orientation="vertical"
app:layout_constraintGuide_begin="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the Logcat:::
2019-08-12 17:14:39.966 9648-9648/com.example.ir_sensor E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ir_sensor, PID: 9648
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ir_sensor/com.example.ir_sensor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at androidx.appcompat.app.AppCompatDelegateImpl.(AppCompatDelegateImpl.java:249)
at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at com.example.ir_sensor.MainActivity.(MainActivity.java:18)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
What can I do?
The error is that you are defining the openMenu and closeMenu variables, outside the onCreate() method. The thing is however that before onCreate you haven't defined your view, so getting the view by id before the view has been defined results in a NullPointerException (the program can't find the variable you were asking for and crashes). So do this:
package com.example.ir_sensor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openMenu = findViewById(R.id.Open_menu);
closeMenu = findViewById(R.id.Close_menu);
}
View openMenu;
View closeMenu;
public void menu() {
if (openMenu.getVisibility() == View.VISIBLE) {
closeMenu.setVisibility(View.GONE);
} else if (closeMenu.getVisibility() == View.GONE){
openMenu.setVisibility(View.VISIBLE);
}
}
}
I was building up a calculator app on Android Studio. When I ran the app on Android Pie device, the following fatal error occurred. As I am new to Android development, I didn't carry on as soon as the error occurred, the main function of the calculator isn't included in the java file.
[logcat]
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: android.view.InflateException: Binary XML file line #15: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f030003 a=-1}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3037)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3172)
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:1906)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #15: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f030003 a=-1}
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f030003 a=-1}
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:794)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:7767)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7958)
at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1997)
at android.support.design.widget.AppBarLayout$LayoutParams.<init>(AppBarLayout.java:743)
at android.support.design.widget.AppBarLayout.generateLayoutParams(AppBarLayout.java:366)
at android.support.design.widget.AppBarLayout.generateLayoutParams(AppBarLayout.java:110)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
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 android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:303)
at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
at com.example.calculator.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:7149)
at android.app.Activity.performCreate(Activity.java:7140)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3017)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3172)
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:1906)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
and in main activity,
package com.example.calculator;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.calculator.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private double valueOne, valueTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
// setContentView(R.layout.activity_main);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
//
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
// fab.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
// }
// });
// Mr. Rigby
// Steven, test
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and what's in the corresponding activity_main.xml file is
<?xml version="1.0" encoding="utf-8"?>
<layout>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<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" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
</layout>
and finally what's in content_main.xml is
<?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:importantForAutofill="no"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/bt_plus"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginStart="21dp"
android:layout_marginTop="57dp"
android:fontFamily="monospace"
android:text="#string/button_plus"
android:textSize="30sp"
android:typeface="normal"
app:layout_constraintEnd_toStartOf="#+id/bt_minus"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tb_valueA" />
<Button
android:id="#+id/bt_minus"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="57dp"
android:fontFamily="monospace"
android:text="#string/button_minus"
android:textSize="30sp"
android:typeface="normal"
app:layout_constraintEnd_toStartOf="#+id/bt_multiply"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/bt_plus"
app:layout_constraintTop_toBottomOf="#+id/tb_valueA" />
<Button
android:id="#+id/bt_multiply"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="57dp"
android:fontFamily="monospace"
android:text="#string/button_multiply"
android:textSize="30sp"
android:typeface="normal"
app:layout_constraintEnd_toStartOf="#+id/bt_divide"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/bt_minus"
app:layout_constraintTop_toBottomOf="#+id/tb_valueB" />
<Button
android:id="#+id/bt_divide"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginStart="20dp"
android:layout_marginTop="57dp"
android:layout_marginEnd="16dp"
android:fontFamily="monospace"
android:text="#string/button_divide"
android:textSize="30sp"
android:typeface="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/bt_multiply"
app:layout_constraintTop_toBottomOf="#+id/tb_valueB" />
<EditText
android:id="#+id/tb_valueA"
android:layout_width="0dp"
android:layout_height="44dp"
android:layout_marginStart="16dp"
android:layout_marginTop="158dp"
android:layout_marginEnd="12dp"
android:ems="10"
android:fontFamily="serif-monospace"
android:hint="#string/valueA"
android:importantForAutofill="no"
android:inputType="numberDecimal"
android:text="#string/valueA"
android:textColor="#android:color/holo_orange_dark"
android:textSize="24sp"
android:typeface="monospace"
app:layout_constraintEnd_toStartOf="#+id/tb_valueB"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/tb_valueB"
android:layout_width="0dp"
android:layout_height="44dp"
android:layout_marginTop="158dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:fontFamily="serif-monospace"
android:hint="#string/valueB"
android:importantForAutofill="no"
android:inputType="numberDecimal"
android:text="#string/valueB"
android:textColor="#android:color/holo_red_dark"
android:textSize="24sp"
android:typeface="monospace"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/tb_valueA"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_result"
android:layout_width="155dp"
android:layout_height="50dp"
android:layout_marginStart="206dp"
android:layout_marginTop="68dp"
android:layout_marginEnd="23dp"
android:layout_marginBottom="132dp"
android:fontFamily="serif-monospace"
android:text="#string/result"
android:textSize="30sp"
android:typeface="monospace"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.222"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/bt_multiply"
app:layout_constraintVertical_bias="0.962" />
</android.support.constraint.ConstraintLayout>
Your layout file can not find ?attr/actionBarSize.
To get action bar size in AppCompat:
?android:attr/actionBarSize
Or you can set manually:
56dp (default)
48dp (landscape)
64dp (sw600dp; i.e. tablet)
I don't know if this helps but you should check your app theme in your styles.xml file. If you have changed it, you have changed it to one that is not compatible with your activity which from what I can see, seems to be a "basic activity" format.
Try changing it to one that descends from Theme.AppCompat for starters. Also, don't forget the define this too and use it on your activity in the android manifest
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
As for a fix that allows you to keep your theme and solve the problem. I will get back to you on that.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
There is the eror message on the monitor:
07-20 17:00:05.234 17194-17194/com.example.champion.courtcounter E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.champion.courtcounter/com.example.champion.courtcounter.MainActivity}: android.view.InflateException: Binary XML file line #69: Error inflating class null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2129)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
at android.app.ActivityThread.access$700(ActivityThread.java:146)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4949)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #69: Error inflating class null
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.champion.courtcounter.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5185)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
at android.app.ActivityThread.access$700(ActivityThread.java:146)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4949)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:101)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:681)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.champion.courtcounter.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5185)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
at android.app.ActivityThread.access$700(ActivityThread.java:146)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4949)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
at dalvik.system.NativeStart.main(Native Method)
There is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="Team A"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:paddingBottom="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="points3"
android:text="+3 points" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp"
android:onClick="points2"
android:text="+2 points" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="point"
android:text="free throw" />
</LinearLayout>
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="#android:color/darker_gray" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="Team B"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:paddingBottom="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="points3B"
android:text="+3 points" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp"
android:onClick="points2B"
android:text="+2 points" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="pointB"
android:text="free throw" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="reset"
android:text="reset" />
</RelativeLayout>
There is my java code:
package com.example.champion.courtcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.example.champion.courtcounter.R;
public class MainActivity extends AppCompatActivity {
int numA=0;
int numB=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
public void points3(View view) {
numA = numA + 3;
displayForTeamA(numA);
}
public void points2(View view) {
numA = numA + 2;
displayForTeamA(numA);
}
public void point(View view) {
numA = numA + 1;
displayForTeamA(numA);
}
public void displayForTeamB(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_b_score);
scoreView.setText(String.valueOf(score));
}
public void points3B(View view) {
numB = numB + 3;
displayForTeamB(numB);
}
public void points2B(View view) {
numB = numB + 2;
displayForTeamB(numB);
}
public void pointB(View view) {
numB = numB + 1;
displayForTeamB(numB);
}
public void reset(View view) {
numB = 0;
displayForTeamB(numB);
numA = 0;
displayForTeamA(numA);
}
}
I can't even open it. When I try to open it's says:"Unfortunately, Court Counter(the name of my app) has stopped.
Thank for help.
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="#android:color/darker_gray" />
Change view with View
I'm trying to make a Drawer Menu but i'm having NullPointer, but i think everything is corect.
I'll post all my code here:
package wagner.com.meuartesanato.home;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import wagner.com.meuartesanato.R;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showHome(null);
TextView textMenu = (TextView) findViewById(R.id.text_menu);
TextView textHome = (TextView) findViewById(R.id.text_home);
TextView textSimulation = (TextView) findViewById(R.id.text_simulation);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
loadDrawer();
}
public void showMenu(View v){
//drawer.openDrawer(GravityCompat.END);
}
public void showHome(View v){
//loadFragment(Home1Fragment.class);
}
public void showSimulation(View v){
Toast.makeText(this,"Mostrar simulação",Toast.LENGTH_SHORT).show();
}
private void loadDrawer(){
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View view = navigationView.getHeaderView(0);
Button about = (Button)view.findViewById(R.id.button_about);
Button howToUse = (Button)view.findViewById(R.id.button_how_to_use);
Button addAccount = (Button)view.findViewById(R.id.button_add_account);
Button changeRegister = (Button)view.findViewById(R.id.button_change_register);
Button changePassword = (Button)view.findViewById(R.id.button_change_password);
Button help = (Button)view.findViewById(R.id.button_help);
Button exit = (Button)view.findViewById(R.id.button_exit);
about.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Mostra tela sobre o app
//loadFragment(SobreFragment.class);
drawer.closeDrawer(GravityCompat.END);
}
});
howToUse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Mostrar como usar",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
addAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Mostrar algo",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
changeRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Alterar cadastro",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
changePassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Alterar cadastro",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
help.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Help",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//fecha a activity, voltando pra tela de login
finish();
}
});
}
private void loadFragment(Class fragmentClass){
try {
Fragment fragment = (Fragment) fragmentClass.newInstance();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_holder, fragment).commit();
}catch (Exception e) {
e.printStackTrace();
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
drawer.closeDrawer(GravityCompat.END);
return false;
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
} else {
drawer.openDrawer(GravityCompat.END);
}
}
}
And my xml files:
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
app:headerLayout="#layout/nav_header_main"
android:fitsSystemWindows="true"
android:background="#color/colorWhite"/>
</android.support.v4.widget.DrawerLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:background="#drawable/degrade"
tools:context="wagner.com.meuartesanato.home.MainActivity">
<LinearLayout
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/top_bar">
<LinearLayout
android:onClick="showMenu"
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#mipmap/menu"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_menu"
android:layout_marginTop="5dp"
android:textSize="12sp"
android:textColor="#FFF"
android:text="Menu"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:onClick="showHome"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#mipmap/home"/>
<TextView
android:id="#+id/text_home"
android:layout_marginTop="5dp"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="Home"
/>
</LinearLayout>
<LinearLayout
android:onClick="showSimulation"
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#mipmap/simulacao"
/>
<TextView
android:id="#+id/text_simulation"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:textSize="12sp"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="Simulação"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#id/top_bar">
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#drawable/shadow"/>
<FrameLayout
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:layout_marginBottom="#dimen/activity_vertical_margin">
</FrameLayout>
</LinearLayout>
</RelativeLayout>
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="#FFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/drawer_avatar"
android:layout_marginTop="20dp"
android:layout_width="115dp"
android:layout_height="115dp" />
<TextView
android:id="#+id/full_name"
android:layout_width="180dp"
android:gravity="center"
android:textColor="#color/colorDarkGray"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorLightGray"
android:text="Sobre o App"
android:id="#+id/button_about"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorWhite"
android:text="Como usar o App"
android:id="#+id/button_how_to_use"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorLightGray"
android:text="Adicionar Conta Bancaria?"
android:id="#+id/button_add_account"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorWhite"
android:text="Alterar Cadastro?"
android:id="#+id/button_change_register"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorLightGray"
android:text="Alterar Senha?"
android:id="#+id/button_change_password"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorWhite"
android:text="Ajuda"
android:id="#+id/button_help"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorLightGray"
android:text="Sair"
android:id="#+id/button_exit"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<TextView
android:layout_marginTop="50dp"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Copyright"
android:textColor="#color/colorGray"
/>
</LinearLayout>
The error log is:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: wagner.com.meuartesanato, PID: 9112
java.lang.RuntimeException: Unable to start activity ComponentInfo{wagner.com.meuartesanato/wagner.com.meuartesanato.home.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)'
on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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.NullPointerException: Attempt to invoke virtual method 'void
android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)'
on a null object reference
at wagner.com.meuartesanato.home.MainActivity.loadDrawer(MainActivity.java:59)
at wagner.com.meuartesanato.home.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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)
I really don't know why i'm getting null pointer exception.
Thanks, for the answers!
The stack trace clearly says what is missing. You could have actually found out the programming error by carefully reviewing your code. Anyways modify the following line in your onCreate method. You were inflating the wrong layout
What it is now - setContentView(R.layout.activity_main);
What you should change it to - setContentView(R.layout.activity_home);