I am new to Android and I started making my first application following tutorials and such. However, when I click the run button it gives me following error on the logcat from which I count not identify where the error is. Hence, here is my code in hope of some advice. Thanks in advance.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText firstnum = (EditText) findViewById(R.id.numberinput);
TextView resultTextView = (TextView) findViewById(R.id.resulttextview);
int num1 = Integer.parseInt(firstnum.getText().toString()) ;
int result = num1 * num1;
resultTextView.setText(result + "");
}
});
}
}
LogCat after fixing the render problem :
7-16 01:27:14.359 10870-10870/com.example.abarimess.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abarimess.myfirstapp, PID: 10870
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abarimess.myfirstapp/com.example.abarimess.myfirstapp.MainActivity}: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x4
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3294)
at android.app.ActivityThread.access$1000(ActivityThread.java:210)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1704)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x4
at android.content.res.TypedArray.getDimensionPixelOffset(TypedArray.java:546)
at android.support.constraint.ConstraintLayout$LayoutParams.<init>(ConstraintLayout.java:2685)
at android.support.constraint.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:1915)
at android.support.constraint.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:476)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:820)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:467)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.abarimess.myfirstapp.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6575)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3294)
at android.app.ActivityThread.access$1000(ActivityThread.java:210)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1704)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
And the layout XML:
<?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"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="73dp">
<TextView
android:id="#+id/textView3"
android:layout_width="360dp"
android:layout_height="119dp"
android:layout_marginTop="50dp"
android:text="#string/square_of_the_number_is"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#android:color/holo_purple"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/numberinput" />
<TextView
android:id="#+id/Resulttextview"
android:layout_width="390dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#android:color/holo_red_dark"
android:text="#string/the_result_is"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff"
android:textSize="50sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/resulttextview"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="30dp"
android:background="#android:color/darker_gray"
android:text="#string/_0"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#color/Optional"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Resulttextview" />
<Button
android:id="#+id/btn"
android:layout_width="370dp"
android:layout_height="80dp"
android:layout_marginTop="50dp"
android:background="?android:attr/colorActivatedHighlight"
android:text="#string/butan"
android:textColor="#android:color/holo_blue_bright"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<EditText
android:id="#+id/numberinput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="#string/number_in_here"
android:inputType="numberDecimal"
android:singleLine="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/resulttextview" />
</android.support.constraint.ConstraintLayout>
the manifest.Xml as requested in the comments :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abarimess.myfirstapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In your layout xml file, you have two separate TextView which are the following.
<TextView
android:id="#+id/Resulttextview"
android:layout_width="390dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#android:color/holo_red_dark"
android:text="#string/the_result_is"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff"
android:textSize="50sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/resulttextview"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="30dp"
android:background="#android:color/darker_gray"
android:text="#string/_0"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#color/Optional"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Resulttextview" />
Name of both text views are identical except the first one has started with a capital R. You might consider the name of the first TextView to something else like resulttextview1 like the following.
<TextView
android:id="#+id/resulttextview1"
android:layout_width="390dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#android:color/holo_red_dark"
android:text="#string/the_result_is"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff"
android:textSize="50sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/resulttextview"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="30dp"
android:background="#android:color/darker_gray"
android:text="#string/_0"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#color/Optional"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/resulttextview1" />
Please avoid naming layout identifiers with capital letters. Use underscores instead (e.g. result_text_view).
Another potential problem in your code is, if there is nothing entered in your EditText the app will crash again with a NullPointerException as the value which will be returned from firstnum.getText().toString() will have the null value.
In this case, you might consider adding a null checking here.
#Override
public void onClick(View v) {
EditText firstnum = (EditText) findViewById(R.id.numberinput);
TextView resultTextView = (TextView) findViewById(R.id.resulttextview);
// Add a null check here for safety
if(firstnum.getText().toString() == null) return;
int num1 = Integer.parseInt(firstnum.getText().toString()) ;
int result = num1 * num1;
resultTextView.setText(result + "");
}
You will get NumberFormatException in case of entering a string in your EditText other than a number. So you might need to handle that case as well. However, this can be ignored in case of handling only numbers in your EditText.
this Is because you should to instance
EditText firstnum = (EditText) findViewById(R.id.numberinput);
Out of method on click listener.
The layout_editor_absoluteX attribute for the resulttextview in your layout XML is referencing spotShadowAlpha, which is an integer which can't be explicitly converted to a dimension, as it doesn't contain any units.
You should update this value to a dimension. For example:
app:layout_editor_absoluteX="30dp"
Obviously, you'll need to change this example up to fit the rest of your layout.
Related
I have created a simple Sign-In activity that looks as follows:
I had like that the whole layout will move up to the bottom of the Sign In button once the E-mail EditText is focused.
I wrote this code:
public class SignInActivity extends AppCompatActivity {
private EditText Et_Email, Et_Password;
private Button Btn_Login;
private ScrollView Sv_Layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
initUI();
}
private void initUI() {
getSupportActionBar().hide();
Sv_Layout = findViewById( R.id.sv_SignInLayout );
Et_Email = findViewById(R.id.et_SignInEmail);
Et_Password = findViewById(R.id.et_SignInPassword);
Btn_Login = findViewById(R.id.btn_SignIn );
Et_Email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean hasfocus) {
if (hasfocus) {
focusOnView();
} else {
Log.e("TAG", "e1 not focused");
}
}
});
}
private final void focusOnView(){
Sv_Layout.post(new Runnable() {
#Override
public void run() {
Sv_Layout.scrollTo(0, Btn_Login.getBottom());
}
});
}
}
The XML is:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/sv_SignInLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite"
android:fillViewport="true"
tools:context=".SignInActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_SignIn"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="8dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/img_signin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.2"
app:layout_constraintHorizontal_bias="0.55"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.48" />
<TextView
android:id="#+id/tv_SignIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="40dp"
android:text="#string/ActivitySignIn_SignIn"
android:textColor="#color/colorLightPurple"
android:textSize="24sp"
app:layout_constraintHeight_percent="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_SignIn"
app:layout_constraintWidth_percent="0.3" />
<EditText
android:id="#+id/et_SignInEmail"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="40dp"
android:background="#drawable/btn_underline"
android:drawableStart="#drawable/ic_signin_email"
android:drawablePadding="10dp"
android:hint="#string/ActivitySignIn_EmailHint"
android:textColor="#color/colorBlackText"
android:inputType="textEmailAddress"
app:layout_constraintHeight_percent="0.06"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_SignIn" />
<EditText
android:id="#+id/et_SignInPassword"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:layout_marginTop="16dp"
android:background="#drawable/btn_underline"
android:drawableStart="#drawable/ic_login_lock"
android:drawablePadding="10dp"
android:hint="#string/ActivitySignIn_PasswordHint"
android:inputType="textPassword"
android:textColor="#color/colorBlackText"
app:layout_constraintHeight_percent="0.06"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/et_SignInEmail" />
<Button
android:id="#+id/btn_SignIn"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginBottom="80dp"
android:layout_marginEnd="40dp"
android:background="#drawable/btn_rounded_purple"
android:text="#string/ActivitySignIn_SignIn"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.08"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Is there any reason that nothing happens? I debugged the app to check if it catches the focus and it does but still, for some reason, it doesn't move the scrollview.
My manifest has android:windowSoftInputMode="adjustPan" now because if I remove it the whole layout shrinks.
Thank you!
Go with Programmatically setting the softInputLayout in onCreate of the Fragment/activity.
OR
Try out any of these,
android:windowSoftInputMode=["stateUnspecified",
"stateUnchanged", "stateHidden",
"stateAlwaysHidden", "stateVisible",
"stateAlwaysVisible", "adjustUnspecified",
"adjustResize", "adjustPan"]
Add below attribute for your activity that holds your layout in the manifest file
android:windowSoftInputMode="adjustResize"
So, for example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.androidxtest">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Also you need to get the Button out of the ScrollView, I wrapped that into RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".SignInActivity">
<ScrollView
android:id="#+id/sv_SignInLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_SignIn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="8dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/img_signin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.2"
app:layout_constraintHorizontal_bias="0.55"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.48" />
<TextView
android:id="#+id/tv_SignIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="40dp"
android:text="#string/ActivitySignIn_SignIn"
android:textColor="#color/colorLightPurple"
android:textSize="24sp"
app:layout_constraintHeight_percent="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_SignIn"
app:layout_constraintWidth_percent="0.3" />
<EditText
android:id="#+id/et_SignInEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="40dp"
android:background="#drawable/btn_underline"
android:drawableStart="#drawable/ic_signin_email"
android:drawablePadding="10dp"
android:hint="#string/ActivitySignIn_EmailHint"
android:textColor="#color/colorBlackText"
android:inputType="textEmailAddress"
app:layout_constraintHeight_percent="0.06"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_SignIn" />
<EditText
android:id="#+id/et_SignInPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:layout_marginTop="16dp"
android:background="#drawable/btn_underline"
android:drawableStart="#drawable/ic_login_lock"
android:drawablePadding="10dp"
android:hint="#string/ActivitySignIn_PasswordHint"
android:inputType="textPassword"
android:textColor="#color/colorBlackText"
app:layout_constraintHeight_percent="0.06"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/et_SignInEmail" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<Button
android:id="#+id/btn_SignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="40dp"
android:layout_marginBottom="80dp"
android:layout_marginEnd="40dp"
android:background="#drawable/btn_rounded_purple"
android:textColor="#color/colorWhite"
android:text="sign in"
android:textAllCaps="false"
android:textSize="20sp" />
</RelativeLayout>
Does any one know how to fix this error, THANK YOU!!
I think something wrong with "public void choose answer" in MainActivity, because then i try press on 1 of 4 buttons I get this error and app crash.
Error message:
FATAL EXCEPTION: main
Process: com.example.braintrainer, PID: 19387
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.braintrainer.MainActivity.chooseAnswer(MainActivity.java:24)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Here all buttons have same onClick (maybe here something wrong?)- android:onClick="chooseAnswer"
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#4CAF50"
android:onClick="start"
android:text="Go!"
android:textSize="60sp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#4CAF50"
android:text="30s"
android:textAlignment="center"
android:textSize="25sp" />
<TextView
android:id="#+id/sumTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="31 + 12"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="25sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#03A9F4"
android:text="0/0"
android:textAlignment="center"
android:textSize="25sp" />
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/gridLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toTopOf="#+id/resultTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="300dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintWidth_max="300dp">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="#+id/button0"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#9C27B0"
android:onClick="chooseAnswer"
android:text="3"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#CDDC39"
android:onClick="chooseAnswer"
android:text="3"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FF5722"
android:onClick="chooseAnswer"
android:text="3"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline2" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#4CAF50"
android:onClick="chooseAnswer"
android:text="3"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toBottomOf="#+id/guideline2" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Correct!"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/startButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here Im trying call buttons from "public void chooseAnswer(View view)" to check if they are working and Im getting their id
MainActivity.java
package com.example.braintrainer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button startButton;
TextView resultTextView;
ArrayList<Integer> answers = new ArrayList<Integer>();
int locationOfCorrectAnswer;
int score = 0;
public void chooseAnswer(View view){
if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))){
Log.i("TAG", String.valueOf(view.getTag()));
}
}
public void start(View view){
startButton.setVisibility(View.INVISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton=(Button)findViewById(R.id.startButton);
TextView sumTextView = (TextView)findViewById(R.id.sumTextView);
Button button0 = (Button)findViewById(R.id.button0);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
resultTextView = (TextView)findViewById(R.id.resultTextView);
Random rand = new Random();
int a = rand.nextInt(21);
int b = rand.nextInt(21);
sumTextView.setText(Integer.toString(a) + " + " + Integer.toString(b));
locationOfCorrectAnswer = rand.nextInt(4);
int incorrectAnswer;
for (int i=0; i<4; i++){
if (i == locationOfCorrectAnswer){
answers.add(a + b);
} else {
incorrectAnswer = rand.nextInt(41);
while (incorrectAnswer == a + b){
incorrectAnswer = rand.nextInt(41);
}
answers.add(incorrectAnswer);
}
}
button0.setText(Integer.toString(answers.get(0)));
button1.setText(Integer.toString(answers.get(1)));
button2.setText(Integer.toString(answers.get(2)));
button3.setText(Integer.toString(answers.get(3)));
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.braintrainer">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You try to check buttons by tag but you didn't set any tags in xml
It's a NullPointerException caused by this. Please check
if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))){
Log.i("TAG", String.valueOf(view.getTag()));
}
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
as view.getTag() return null.
try to set the tag to your Buttons like this:
<Button
android:id="#+id/button0"
android:tag="1" // this line
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#9C27B0"
android:onClick="chooseAnswer"
android:text="3"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintEnd_toStartOf="#+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I am relatively new to coding but have some experience in Java and Android. I have a really basic problem that I have done over and over again but for some reason, this occurrence doesn't work!
I have an edittext field which is populated by the user. When an enter button is pressed the app then takes this and saves it as a String. The String is then used to populate the TextView with the user input.
I have created the findviewbyids, I have set the String to equal the edittext input and then set the text in the text view to be the String as per the code below. I have checked previous apps I have made and this has always worked before...
package com.example.android.golfhandicap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText playerName;
TextView playerOneName;
String name;
int handicap;
EditText playerHandicap;
TextView playerOneHcp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
}
//set view by IDs
public void views() {
playerName = findViewById(R.id.inputPlayerName);
playerOneName = findViewById(R.id.playerOneName);
playerHandicap = findViewById(R.id.inputPlayerHcp);
playerOneHcp = findViewById(R.id.playerOneHcp);
}
/**
* Add the codes for the button to navigate around the app
*/
public void enterScore1(View view) {
setContentView(R.layout.front_page);
}
public void addPlayer(View view) {
setContentView(R.layout.new_player);
}
public void addScore(View view) {
setContentView(R.layout.input_page);
}
public void enterPlayer(View view) {
views();
name = playerName.getText().toString();
//handicap = Integer.parseInt(playerHandicap.getText().toString());
playerOneName.setText(name);
//playerOneHcp.setText(handicap);
Toast.makeText(this, "player name is " + name + " and handicap is
" + handicap , Toast.LENGTH_SHORT).show();
setContentView(R.layout.front_page);
}
}
The XML for the page with the text view I want to display the String in:
<?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:visibility="visible"
tools:context=".MainActivity">
<TextView
android:id="#+id/appName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="Golf handicap"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible"
android:editable="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/playerOneName"
android:layout_width="136dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:editable="false"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appName2" />
<TextView
android:id="#+id/playerOneName2"
android:editable="false"
android:layout_width="136dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/playerOneName3" />
<TextView
android:id="#+id/playerOneName3"
android:editable="false"
android:layout_width="136dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/playerOneName" />
<Button
android:id="#+id/addScore"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:background="#android:color/holo_green_light"
android:onClick="addScore"
android:text="Add new score"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/addPlayer" />
<Button
android:id="#+id/addPlayer"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:background="#android:color/holo_green_light"
android:onClick="addPlayer"
android:text="Add new player"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/addScore"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/playerOneHcp"
android:layout_width="43dp"
android:layout_height="30dp"
android:layout_marginStart="31dp"
android:layout_marginTop="32dp"
android:editable="false"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:visibility="visible"
app:layout_constraintStart_toEndOf="#+id/playerOneName"
app:layout_constraintTop_toBottomOf="#+id/appName2" />
<TextView
android:id="#+id/editText2"
android:editable="false"
android:layout_width="43dp"
android:layout_height="30dp"
android:layout_marginStart="31dp"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="#+id/playerOneName2"
app:layout_constraintTop_toBottomOf="#+id/editText3" />
<TextView
android:id="#+id/editText3"
android:editable="false"
android:layout_width="43dp"
android:layout_height="30dp"
android:layout_marginStart="31dp"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="#+id/playerOneName3"
app:layout_constraintTop_toBottomOf="#+id/playerOneHcp" />
</android.support.constraint.ConstraintLayout>
The XML for the layout with the edittext which I get the user input from:
<?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">
<TextView
android:id="#+id/enterHcpText"
android:layout_width="136dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:editable="false"
android:text="Current handicap :"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enterAgeText" />
<TextView
android:id="#+id/enterAgeText"
android:layout_width="136dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:editable="false"
android:text="Player age :"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enterNameText" />
<TextView
android:id="#+id/enterNameText"
android:layout_width="136dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:editable="false"
android:text="Player name :"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appName3" />
<TextView
android:id="#+id/appName3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:editable="false"
android:text="Golf handicap"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/inputPlayerName"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="33dp"
android:maxLength="25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appName3" />
<EditText
android:id="#+id/inputPlayerAge"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="33dp"
android:inputType="number"
android:maxLength="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputPlayerName" />
<EditText
android:id="#+id/inputPlayerHcp"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="33dp"
android:inputType="number"
android:maxLength="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputPlayerAge" />
<Button
android:id="#+id/enterPlayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="96dp"
android:layout_marginEnd="148dp"
android:onClick="enterPlayer"
android:text="Enter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputPlayerHcp" />
</android.support.constraint.ConstraintLayout>
If I comment out the line p1Name.setText(name); then it works fine, I have a toast to check the name is taken and name is set as the input. So there seems to be something wrong with the p1Name. I have checked and triple checked all the IDs are correct.
I have spent over an hour now trying to figure it out and can't see what I am doing wrong!
This is the lines I get on logcat (These are all the lines shown in red)
2019-01-24 23:29:39.376 9545-9545/com.example.android.golfhandicap E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.golfhandicap, PID: 9545
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
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.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.android.golfhandicap.MainActivity.enterPlayer(MainActivity.java:65)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
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)
Any help would be gratefully received.
The problem is simply because of the following:
you haven't bind the view with the findViewById. This is because of the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
// Here you haven't bind the view.
}
you should directly bind the view with your views() method. So, change the code to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
// bind the view.
views();
}
You're changing the layout view each time a button is clicked with this method:
public void addScore(View view) {
setContentView(R.layout.input_page);
}
which is become one of the source for your problem. First, it's not recommended because you're redrawing entire layout when calling setContentView. Second, you need to make sure all the layout onClick is implemented in your code.
Last but not the least, you better using setOnClickListener in code instead onClick attribute in layout. It is because you should separate between logic and UI.
I'm developing an app for my school with a timetable and a little calculator
Here's the manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.markwitt.schul_app">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:launchMode="singleTop"
android:minSdkVersion="20"
android:supportsRtl="true"
android:targetSdkVersion="25"
android:theme="#style/AppTheme">
<activity android:name=".Stundenplan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Vertretungsplan" />
<activity android:name=".Rechner" />
<activity android:name=".Overlay"></activity>
</application>
Here's the Launcher activity activity_stundenplan.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<Button
android:text="Rechner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonrechner"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="44dp"
android:layout_marginStart="44dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonvertr"
android:text="Vertretungsplan"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/Tabelle"
android:layout_alignEnd="#+id/Tabelle" />
<TableLayout
android:id="#+id/Tabelle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.markwitt.schul_app.Stundenplan"
android:background="#color/white"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/r10"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r20"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r30"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r40"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r50"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r60"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/r11"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView android:text="#string/r21" android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r31"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r41"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r51"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r61"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/r12"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r22"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r32"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r42"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r52"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r62"
android:padding="10dp"
android:background="#drawable/cell_shape"
//more TableRows
</TableLayout>
</Relativelayout>
I've made an Intent in Stundenplan.java:
rechner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(Stundenplan.this, Rechner.class));
}
});
The Problem is that everytime I debug-launch the app on my Xperia Z3 and press the button to open the "Rechner"-Activity, the app closes and it shows me the following error:(sry for the bad format)
I/OpenGLRenderer: Initialized EGL, version 1.4
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy#f97be6e time:6061206
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy#f97be6e time:6065998
I/Timeline: Timeline: Activity_launch_request id:com.example.markwitt.schul_app time:6074313
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.markwitt.schul_app, PID: 22562
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.markwitt.schul_app/com.example.markwitt.schul_app.Rechner}: 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:2375)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
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 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
at com.example.markwitt.schul_app.Rechner.<init>(Rechner.java:16)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
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/Process: Sending signal. PID: 22562 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
My Rechner.java and my activity_rechner look like this:
package com.example.markwitt.schul_app;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class Rechner extends AppCompatActivity {
private RadioGroup radioGroup;
private RadioButton checked;
private Button los;
public String mode;
EditText number1 = (EditText) findViewById(R.id.nummer1);
EditText number2 = (EditText) findViewById(R.id.nummer2);
EditText ergebnis = (EditText) findViewById(R.id.ergebnis);
public int text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechner);
addListenerOnButton();
}
public void addListenerOnButton() {
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
los = (Button) findViewById(R.id.button5);
los.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
checked = (RadioButton) findViewById(selectedId);
switch (checked.getId()){
case R.id.plus: mode = "+";
case R.id.minus: mode = "-";
case R.id.mal: mode = "*";
case R.id.geteilt: mode = ":";
case R.id.fakultaet: mode = "fak";
}
text1 = Integer.parseInt(number1.getText().toString());
int text2 = Integer.parseInt(number2.getText().toString());
switch (mode){
case "+": ergebnis.setText(text1 + text2);
case "-": ergebnis.setText(text1 - text2);
case "*": ergebnis.setText(text1 * text2);
case ":": ergebnis.setText(text1 / text2);
case "fak": ergebnis.setText(Long.toString(fakultaet(text1)));
}
}
});
}
static long fakultaet(int n) {
int ergebnis = 1;
for (int i = 1; i <= n; i++) {
ergebnis = ergebnis * i;
}
return ergebnis;
}
}
...
<?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_durchschnitt"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.markwitt.schul_app.Rechner">
<EditText
android:id="#+id/nummer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:ems="10"
android:hint="Nummer 1"
android:inputType="number"
android:maxLines="1"
android:textAlignment="center" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/ergebnis"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:text="=" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/nummer1"
android:layout_marginEnd="42dp"
android:layout_marginRight="42dp"
android:layout_marginTop="69dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/mal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/geteilt"
android:layout_alignBottom="#+id/geteilt"
android:layout_toLeftOf="#+id/geteilt"
android:layout_toStartOf="#+id/geteilt"
android:layout_weight="1"
android:text="* "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/fakultaet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/mal"
android:layout_alignBottom="#+id/mal"
android:layout_toLeftOf="#+id/mal"
android:layout_toStartOf="#+id/mal"
android:layout_weight="1"
android:text="Fakultät "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minus"
android:layout_alignBottom="#+id/minus"
android:layout_toEndOf="#+id/nummer1"
android:layout_toRightOf="#+id/nummer1"
android:layout_weight="1"
android:text="+ "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/geteilt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/radioGroup"
android:layout_toEndOf="#+id/radioGroup"
android:layout_toRightOf="#+id/radioGroup"
android:layout_weight="1"
android:text=": "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/geteilt"
android:layout_alignBottom="#+id/geteilt"
android:layout_toLeftOf="#+id/plus"
android:layout_toStartOf="#+id/plus"
android:layout_weight="1"
android:text="- "
android:textAllCaps="true"
android:textStyle="bold" />
</RadioGroup>
<EditText
android:id="#+id/ergebnis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nummer2"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/nummer2"
android:layout_marginBottom="61dp"
android:ems="10"
android:hint="Ergebnis"
android:inputType="number"
android:textAlignment="center" />
<EditText
android:id="#+id/nummer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nummer1"
android:layout_alignStart="#+id/nummer1"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Nummer 2"
android:inputType="number"
android:textAlignment="center" />
</RelativeLayout>
Can you please give me an answer what to do? It does the same on my phone and on my emulator
If you have questions, contact me please!
PS: I have to finish it till the 12th December
You cannot use findviewbyid before you set the content view. It returns null and that's your exception.
Change your code to something like that:
EditText number1;
EditText number2;
EditText ergebnis;
public int text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechner);
number1 = (EditText) findViewById(R.id.nummer1);
number2 = (EditText) findViewById(R.id.nummer2);
ergebnis = (EditText) findViewById(R.id.ergebnis);
addListenerOnButton();
}
Also, Ill recommend you to read the android code style recommendations.
https://source.android.com/source/code-style.html
I'm making a pretty basic 6-7 activity app, trying to keep it really simple. I've succesfully added 4 other instances where at the bottom of the app you press "NEXT" and it takes you to the next activity in the app. However, I'm stuck and can't figure out how to continue. I've tried debugging and can't figure out what the error message means. THanks for your help.
The error message from Logcat:
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.example.android.budgeit10.expenses.fourthNext(expenses.java:86)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
JAVA:
package com.example.android.budgeit10;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class expenses extends AppCompatActivity {
AppCompatActivity appCompatActivity;
EditText housingExpense;
EditText foodExpense;
EditText transportExpense;
EditText otherExpense;
Button expensesButton;
TextView addExpenses;
EditText income;
double housing, food, transport, other, sum;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expenses);
housingExpense = (EditText) findViewById(R.id.housingExpense);
foodExpense = (EditText) findViewById(R.id.foodExpense);
transportExpense = (EditText) findViewById(R.id.transportExpense);
otherExpense = (EditText) findViewById(R.id.otherExpense);
expensesButton = (Button) findViewById(R.id.expensesButton);
addExpenses = (TextView) findViewById(R.id.totalMonthlyExpenses);
income = (EditText) findViewById(R.id.monthlyIncome);
expensesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
housing = Double.parseDouble(housingExpense.getText().toString());
try {
Double.parseDouble(housingExpense.getText().toString());
} catch (NumberFormatException nfe) {
}
food = Double.parseDouble(foodExpense.getText().toString());
transport = Double.parseDouble(transportExpense.getText().toString());
other = Double.parseDouble(otherExpense.getText().toString());
sum = housing + food + transport + other;
addExpenses.setText(Double.toString(sum));
}
});
expensesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TextUtils.isEmpty(housingExpense.getText().toString()) ||
TextUtils.isEmpty(foodExpense.getText().toString()) ||
TextUtils.isEmpty(otherExpense.getText().toString()) ||
TextUtils.isEmpty(transportExpense.getText().toString())) {
addExpenses.setText("Please fill all fields."); // if any of the fields is empty, add nothing to textview
} else {
Double housing = Double.parseDouble(housingExpense.getText().toString());
Double food = Double.parseDouble(foodExpense.getText().toString());
Double transport = Double.parseDouble(transportExpense.getText().toString());
Double other = Double.parseDouble(otherExpense.getText().toString());
Double sum = housing + food + transport + other;
addExpenses.setText(Double.toString(sum));
}
}
});
}
public void fourthNext(View view) {
Intent intent = new Intent(this, Goals.class);
TextView textView = (TextView) findViewById(R.id.totalMonthlyIncome);
String income = textView.getText().toString();
startActivity(intent);
}
XML from Button:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.budgeit10.incomePage">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="MONTHLY EXPENSES"
android:textSize="24dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/totalMonthlyIncome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="Enter Monthly Income"
android:textSize="22dp"
android:textStyle="bold" />
<EditText
android:id="#+id/monthlyIncome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/income_on_expenses"
android:inputType="phone"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#2E7D32" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="HOUSING"
android:textSize="22dp"
android:textStyle="bold" />
<EditText
android:id="#+id/housingExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/housing"
android:inputType="phone"
android:paddingLeft="16dp"
android:paddingRight="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:paddingTop="16dp"
android:text="FOOD"
android:textSize="22dp"
android:textStyle="bold" />
<EditText
android:id="#+id/foodExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/food"
android:inputType="phone"
android:paddingLeft="16dp"
android:paddingRight="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:paddingTop="16dp"
android:text="TRANSPORTATION"
android:textSize="22dp"
android:textStyle="bold" />
<EditText
android:id="#+id/transportExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/transport"
android:inputType="phone"
android:paddingLeft="16dp"
android:paddingRight="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:paddingTop="16dp"
android:text="EVERYTHING ELSE"
android:textSize="22dp"
android:textStyle="bold" />
<EditText
android:id="#+id/otherExpense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/other"
android:inputType="phone"
android:paddingLeft="16dp"
android:paddingRight="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#2E7D32" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="16dp">
<Button
android:id="#+id/expensesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#C8E6C9"
android:onClick="submitOrder"
android:padding="16dp"
android:text="TOTAL EXPENSES"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="#+id/totalMonthlyExpenses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/monthly_expenses"
android:inputType="phone"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="Extra $ : "
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/extra_cash"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/extra_cash"
android:inputType="phone"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textSize="16dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/fourthNext"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:background="#C8E6C9"
android:onClick="fourthNext"
android:text="NEXT"
android:textSize="34dp">
</Button>
</RelativeLayout>
</LinearLayout>
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.budgeit10">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PageTwo" />
<activity android:name=".incomePage" />
<activity android:name=".expenses" />
<activity android:name=".Goals" />
</application>
You should have a TextView with id 'totalMonthlyIncome' in your XML layout. (According to the Java code) Just make sure that's there and it will solve the issue.
"Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.example.android.budgeit10.expenses.fourthNext(expenses.java:86)"
According to the logcat error is in expenses.java - line 86