I know that this question looks similar to others on SO but i have tried all the suggestions in the other similar questions and haven't had any success.
I'm getting this error:
2020-08-03 20:41:21.372 8916-8916/com.example.spacing E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.spacing, PID: 8916
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.spacing/com.example.spacing.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:159)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:675)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.example.spacing.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2020-08-03 20:41:21.411 8916-8916/com.example.spacing I/Process: Sending signal. PID: 8916 SIG: 9
on this code:
package com.example.spacing;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public int firstValue;
EditText first;
String firstString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first = (EditText)findViewById(R.id.firstNum);
firstString = first.getText().toString();
Button Go = (Button) findViewById(R.id.goButton);
Go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firstValue = Integer.parseInt(firstString);
System.out.print(firstValue);
}
});
}
}
Here is the 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/goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.898" />
<TextView
android:id="#+id/FirstAttribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/spacedMaterial"
app:layout_constraintBottom_toTopOf="#+id/goButton"
app:layout_constraintEnd_toStartOf="#+id/firstNum"
app:layout_constraintHorizontal_bias="0.457"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.043" />
<EditText
android:id="#+id/firstNum"
android:layout_width="165dp"
android:layout_height="40dp"
android:ems="10"
android:hint="#string/inMM"
android:importantForAutofill="no"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/FirstAttribute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/FirstAttribute"
app:layout_constraintVertical_bias="0.476" />
<TextView
android:id="#+id/secondAttribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/amount"
app:layout_constraintBottom_toTopOf="#+id/goButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.073"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131" />
<EditText
android:id="#+id/secondNumber"
android:layout_width="165dp"
android:layout_height="40dp"
android:ems="10"
android:hint="#string/number"
android:importantForAutofill="no"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/secondAttribute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/secondAttribute" />
<TextView
android:id="#+id/thirdAttribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/areaWidth"
app:layout_constraintBottom_toTopOf="#+id/goButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.068"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.218" />
<EditText
android:id="#+id/thirdNumber"
android:layout_height="40dp"
android:layout_width="165dp"
android:ems="10"
android:hint="#string/inMM"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/thirdAttribute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/thirdAttribute"
app:layout_constraintVertical_bias="0.476"
android:importantForAutofill="no" />
</androidx.constraintlayout.widget.ConstraintLayout>
Suggestions i have tried:
I have tried changing the init for the edit text to before and after on-create.
I have made sure that all ids are correct and are referencing the right thing.
And about 5 other things that i cant recall but none of them worked when i tried to build.
Any help is appreciated.
Problem is:
you are trying to initialize the "firstString" variable OUTSIDE of the click listener and as soon as the Activity is created, which means the variable will always be a null object because you did not actually write something in the EditText.
Simply initialize the String inside the ClickListener!
` package com.example.spacing;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public int firstValue;
EditText first;
String firstString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first = (EditText)findViewById(R.id.firstNum);
Button Go = (Button) findViewById(R.id.goButton);
Go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firstString = first.getText().toString();
firstValue = Integer.parseInt(firstString);
System.out.print(firstValue);
}
});
}
}`
Related
package com.example.firstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onBtnTxt(View view){
TextView txtApp = findViewById(R.id.txtMsg);
EditText inpTxt = findViewById(R.id.input);
txtApp.setText(inpTxt.getText().toString());
}
}
this is java code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/txtMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome!!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.339" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick= "onBtnTxt"
android:text="Click"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextTextPersonName"
app:layout_constraintVertical_bias="0.286" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Your name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtMsg" />
</androidx.constraintlayout.widget.ConstraintLayout>
this is xml code
2023-01-25 22:07:15.960 9062-9062/com.example.firstapp D/AndroidRuntime: Shutting down VM
2023-01-25 22:07:15.962 9062-9062/com.example.firstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firstapp, PID: 9062
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446)
at android.view.View.performClick(View.java:6597)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.onKeyUp(View.java:13216)
at android.widget.TextView.onKeyUp(TextView.java:7821)
at android.view.KeyEvent.dispatch(KeyEvent.java:2716)
at android.view.View.dispatchKeyEvent(View.java:12450)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1896)
at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:428)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1820)
at android.app.Activity.dispatchKeyEvent(Activity.java:3360)
at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:122)
at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:140)
at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:599)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:3089)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:342)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5037)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4905)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4479)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4445)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4585)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4453)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4642)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4479)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4445)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4453)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4479)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4445)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4618)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4779)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2571)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2081)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2072)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2548)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:6669)
2023-01-25 22:07:15.963 9062-9062/com.example.firstapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
... 52 more
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditText
at com.example.firstapp.MainActivity.onBtnTxt(MainActivity.java:25)
... 54 more
this is the error log
I was following android development for beginner course by freecodecamp from youtube.
My android studio is properly installed. I have installed "pie" with api level of 28 as the virtual device. After doing everything exactly same i get this error. My app suddenly crashes and shows fatal error.
Please help Im completely new in this.
If you'll scroll down in your stack starce you will see this:
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditText
at com.example.firstapp.MainActivity.onBtnTxt(MainActivity.java:25)
In your onBtnTxt(View view) method you wrote
EditText inpTxt = findViewById(R.id.input);
but in the layout file you gave this id (id.input) to the root view which is a constraintlayout, not an EditText
change it to
EditText inpTxt = findViewById(R.id.editTextTextPersonName);
I am making a little app were the user needs te scan a QRcode from a product to register how much of this product he used.
I want the user to have the possbility to put in the value from the code manually, becuase the QR is printed on a piece of papier that gets easily damaged.
I have implemented a barcodescanner using ZXing, and I am able to open this.
However, when I scan a code and want to assing the value of it to my EditText field, I get the folowing error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: be.jacops.juniorvandamme.verbruikpxs, PID: 29933
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at be.jacops.juniorvandamme.verbruikpxs.ActivityQr.handleResult(ActivityQr.java:59)
at me.dm7.barcodescanner.zxing.ZXingScannerView$1.run(ZXingScannerView.java:148)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
This also results in the app crashing.
The code from this activity is the folowing:
package be.jacops.juniorvandamme.verbruikpxs;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ActivityQr extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
Button btnToUsage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
btnToUsage = findViewById(R.id.btnToUsage);
btnToUsage.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
EditText txtNumber = findViewById(R.id.txtNumber);
String strNumber = txtNumber.getText().toString();
if(TextUtils.isEmpty(strNumber)){
txtNumber.setError("Geef een Bobijnnummer in, of scan de QR code.");
return;
} else {
Intent i = new Intent(getApplicationContext(), ActivityUsage.class);
startActivity(i);
}
}
});
}
public void qrScanner(View view){
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
public void onPause(){
super.onPause();
Intent i = new Intent(getApplicationContext(), ActivityUsage.class);
startActivity(i);
}
public void handleResult(Result rawResult){
final EditText txtNumber = findViewById(R.id.txtNumber);
String txtTemp = rawResult.getText().toString();
Log.e("QRcapture", txtTemp);
txtNumber.setText(txtTemp);
}
}
The XML file for this is the folowing:
<?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=".ActivityQr">
<Button
android:id="#+id/btnToUsage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Volgende"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.68" />
<Button
android:id="#+id/btnToScanner"
android:layout_width="165dp"
android:layout_height="wrap_content"
android:layout_marginBottom="56dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Gebruik de scanner"
android:onClick="qrScanner"
app:layout_constraintBottom_toTopOf="#+id/btnToUsage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
android:text="Geef het bobijnnummer in."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="text"
android:text="#string/bobijnnummer"
app:layout_constraintBottom_toTopOf="#+id/btnToScanner"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.697" />
</android.support.constraint.ConstraintLayout>
I've been looking for a fix all day searching on multiple sites (one of which is here). I can't seem to find an answer.
try declaring your fields outside any particular method. In this case declare your EditText outside onCreate and assign the reference inside it rather than inside the listener. I think this is a scope error as the reference is not assigned at runtime when the call is made.
This question already has answers here:
Null pointer Exception - findViewById()
(12 answers)
Closed 5 years ago.
Hi i'm having a problem with my app, its crashing when ever i press button called "Riders". The error that i'm getting is:
12-13 10:40:03.467 16463-16463/bskerritt.student.ncirl.ie.saddleup E/AndroidRuntime: FATAL EXCEPTION: main
Process: bskerritt.student.ncirl.ie.saddleup, PID: 16463
java.lang.RuntimeException: Unable to start activity ComponentInfo{bskerritt.student.ncirl.ie.saddleup/bskerritt.student.ncirl.ie.saddleup.RiderActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
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 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at bskerritt.student.ncirl.ie.saddleup.RiderActivity.onCreate(RiderActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
I'v been trying to solve this for ages now. The error is referencing the error to the page called RiderActivity:
package bskerritt.student.ncirl.ie.saddleup;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class RiderActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Contact> list = new ArrayList<Contact>();
int[] image_id = { R.drawable.lilyheadshot, R.drawable.aaronheadshot, R.drawable.willowheadshot,
R.drawable.kidheadshot1, R.drawable.kidheadshot2, R.drawable.kidheadshot3, R.drawable.kidheadshot4,
R.drawable.kidheadshot5 };
String[] name, mobile, address;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rider);
name = getResources().getStringArray(R.array.rider_name);
mobile = getResources().getStringArray(R.array.rider_mobile);
address = getResources().getStringArray(R.array.rider_address);
int count = 0;
for(String Name: name) {
Contact contact = new Contact(image_id[count], Name, mobile[count], address[count]);
count++;
list.add(contact);
}
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new ContactAdapter(list);
recyclerView.setAdapter (adapter);
}
}
I'm using android studio, any help will be appreciated. I was trying few solutions that are on stackoverflow but non worked. Please help. Thanks !
This is the code for my activity_rider:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bskerritt.student.ncirl.ie.saddleup.RiderActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/RecyclerView">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
>
<ImageView
android:id="#+id/rider_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/lilyheadshot" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/rider_image"
android:text="Lily Skerritt"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_alignParentTop="true"
android:id="#+id/rider_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rider_name"
android:layout_toRightOf="#id/rider_image"
android:text="0872287760"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="#+id/rider_mobile"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rider_mobile"
android:layout_toRightOf="#id/rider_image"
android:text="Laytown, Meath"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="#+id/rider_address"
/>
</RelativeLayout>
</android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/button5"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:text="#string/addRiderBtn"
android:textColor="#color/suPrimary"
android:textSize="18sp"
android:textStyle="bold" />
Also regarding the other code provided with context, i'm getting this error: http://prntscr.com/hmw2yi
Okay so the first error has been fixed, changing the R.id.RecyclerView did it but for some reason the error now occurs on different line:
12-13 11:10:11.590 2486-2486/bskerritt.student.ncirl.ie.saddleup E/AndroidRuntime: FATAL EXCEPTION: main
Process: bskerritt.student.ncirl.ie.saddleup, PID: 2486
java.lang.RuntimeException: Unable to start activity ComponentInfo{bskerritt.student.ncirl.ie.saddleup/bskerritt.student.ncirl.ie.saddleup.RiderActivity}: android.view.InflateException: Binary XML file line #15: RecyclerView has no LayoutManager
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
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: android.view.InflateException: Binary XML file line #15: RecyclerView has no LayoutManager
Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager
at android.support.v7.widget.RecyclerView.generateLayoutParams(RecyclerView.java:3996)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:860)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at bskerritt.student.ncirl.ie.saddleup.RiderActivity.onCreate(RiderActivity.java:36)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
12-13 11:11:00.875 2486-2494/bskerritt.student.ncirl.ie.saddleup W/art: Suspending all threads took: 6.024ms
Use this
recyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
Instead of this
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
EDIT
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bskerritt.student.ncirl.ie.saddleup.RiderActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/RecyclerView"/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
>
<ImageView
android:id="#+id/rider_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/lilyheadshot" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/rider_image"
android:text="Lily Skerritt"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_alignParentTop="true"
android:id="#+id/rider_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rider_name"
android:layout_toRightOf="#id/rider_image"
android:text="0872287760"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="#+id/rider_mobile"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rider_mobile"
android:layout_toRightOf="#id/rider_image"
android:text="Laytown, Meath"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="#+id/rider_address"
/>
</RelativeLayout>
<Button
android:id="#+id/button5"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:text="#string/addRiderBtn"
android:textColor="#color/suPrimary"
android:textSize="18sp"
android:textStyle="bold" />
Please specify you are using same id in .xml file of recycler view
Second thing-:
package bskerritt.student.ncirl.ie.saddleup;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class RiderActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
private Context context=this;
ArrayList<Contact> list = new ArrayList<Contact>();
int[] image_id = {
R.drawable.lilyheadshot,
R.drawable.aaronheadshot,
R.drawable.willowheadshot,
R.drawable.kidheadshot1,
R.drawable.kidheadshot2,
R.drawable.kidheadshot3,
R.drawable.kidheadshot4,
R.drawable.kidheadshot5
};
String[] name, mobile, address;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rider);
name = getResources().getStringArray(R.array.rider_name);
mobile = getResources().getStringArray(R.array.rider_mobile);
address = getResources().getStringArray(R.array.rider_address);
int count = 0;
for(String Name: name){
Contact contact = new Contact(image_id[count], Name, mobile[count], address[count]);
count++;
list.add(contact);
}
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new ContactAdapter(list);
recyclerView.setAdapter (adapter);
}}'
Use context in case of this as I have changed the code.
I created a new activity in my android studio app and I have a button that performs an action when click. I am not sure what's causing the error as this happens when I actually run the activity. If I remove the onClick code, the activity runs. Thanks for any help. I'm getting this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.masesk.kalkulator, PID: 4102
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.masesk.kalkulator/com.masesk.kalkulator.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 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.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.masesk.kalkulator.Main2Activity.onCreate(Main2Activity.java:29)
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)
This is my Main2Activity.java code:
package com.masesk.kalkulator;
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.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class Main2Activity extends AppCompatActivity {
private ToggleButton textToBinary;
private Button trace;
private EditText text;
private EditText binary;
private String textInput;
private String binaryInput;
StringBuilder binaryText = new StringBuilder();
#Override
protected void onCreate(Bundle savedInstanceState) {
this.textToBinary = (ToggleButton)findViewById(R.id.textToBinary);
this.trace = (Button)findViewById(R.id.trace);
this.text = (EditText)findViewById(R.id.text);
this.binary = (EditText)findViewById(R.id.binary);
trace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(textToBinary.isActivated() == false){
textInput = text.getText().toString();
byte[] bytes = textInput.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binaryText.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
binaryText.append(' ');
}
}
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public void onResume(){
this.binary = (EditText)findViewById(R.id.binary);
binary.setText(binaryText.toString(), TextView.BufferType.EDITABLE);
}
}
and here is my XML code
<?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:id="#+id/content_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.masesk.kalkulator.Main2Activity"
tools:showIn="#layout/activity_main2">
<ToggleButton
android:text="ToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/textToBinary"
android:layout_alignParentEnd="true"
android:textOff="Binary To Text"
android:textOn="Text to Binary" />
<TextView
android:text="Text:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textToBinary"
android:layout_alignParentStart="true"
android:id="#+id/textView2"
android:layout_alignParentEnd="true" />
<EditText
android:background="#drawable/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true"
android:id="#+id/text"
android:layout_alignParentEnd="true" />
<TextView
android:text="Binary Code:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:layout_alignParentStart="true"
android:id="#+id/textView3" />
<EditText
android:background="#drawable/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:layout_below="#+id/textView3"
android:layout_alignParentStart="true"
android:id="#+id/binary"
android:layout_alignParentEnd="true" />
<Button
android:text="convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/binary"
android:layout_alignParentStart="true"
android:layout_marginTop="14dp"
android:id="#+id/trace"
android:layout_alignParentEnd="true" />
</RelativeLayout>
In the OnCreate method, you should always first put
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
This is because the view is not inflated until you have set it with setContentView. This means any findViewById call before will return null.
Additionally, you must also set the super.onResume(...) call at the very beginning of onResume(), as the Android documentation states. This also goes for the other lifecycle methods as well. This should also solve your setText problem.
I am attempting to get Strings from 2 Edittexts within a Dialog Fragment, but my app keeps crashing when the activity starts. It is some sort of issue with being able to bind the edittexts from the dialog. I am not sure of the correct way to bind them.
Here is the crash:
09-24 11:34:30.366 16147-16147/com.epicodus.concertaid E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.epicodus.concertaid, PID: 16147
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.epicodus.concertaid/com.epicodus.concertaid.ui.UserProfileActivity}: java.lang.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
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.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
at butterknife.ButterKnife.bind(ButterKnife.java:322)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37)
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)
Caused by: java.lang.IllegalStateException: Required view 'userEmailEditText' with ID 2131558627 for field 'mUserEmailEditText' was not found. If this view is optional add '#Nullable' annotation.
at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:15)
at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:8)
at butterknife.ButterKnife.bind(ButterKnife.java:319)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37)
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)
And here is the Activity:
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.epicodus.concertaid.Constants;
import com.epicodus.concertaid.R;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import butterknife.Bind;
import butterknife.ButterKnife;
public class UserProfileActivity extends AppCompatActivity implements View.OnClickListener {
#Bind(R.id.summaryTextView) TextView mSummaryTextView;
#Bind(R.id.deleteAccountButton) Button mDeleteAccountButton;
#Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
#Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;
private Firebase mFirebaseRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
ButterKnife.bind(this);
mDeleteAccountButton.setOnClickListener(this);
//GET REFERENCE TO FIREBASE
mFirebaseRef = new Firebase(Constants.FIREBASE_URL);
//SETS FONT FOR TITLE
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MUSICNET.ttf");
mSummaryTextView.setTypeface(tf);
}
#Override
public void onClick(View view) {
if(view == mDeleteAccountButton) {
createAlertDialog();
}
}
public void deleteUser(String userEmail, String userPassword) {
Firebase.ResultHandler handler = new Firebase.ResultHandler() {
#Override
public void onSuccess() {
Toast.makeText(UserProfileActivity.this, "User deleted Successflly", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FirebaseError firebaseError) {
Toast.makeText(UserProfileActivity.this, "There was an error, please try again", Toast.LENGTH_LONG).show();
}
};
mFirebaseRef.removeUser(userEmail, userPassword, handler );
}
public void createAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileActivity.this);
LayoutInflater inflater = UserProfileActivity.this.getLayoutInflater();
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
String userEmail = mUserEmailEditText.getText().toString();
String userPassword = mUserPasswordEditText.getText().toString();
deleteUser(userEmail, userPassword);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
This is the layout for the activity
<?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"
tools:context="com.epicodus.concertaid.ui.UserProfileActivity"
android:id="#+id/relativeLayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:src="#drawable/background1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account Details"
android:textColor="#0288D1"
android:id="#+id/summaryTextView"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Photo"
android:textColor="#0288D1"
android:background="#drawable/buttonshape"
android:id="#+id/addPhotoButton"
android:layout_below="#+id/summaryTextView"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="25dp" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/userPhotoImageView"
android:layout_marginTop="25dp"
android:layout_below="#+id/addPhotoButton"
android:layout_centerHorizontal="true"
android:contentDescription="#string/current_user_s_image"
android:src="#drawable/blank_user" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Email"
android:textColor="#0288D1"
android:id="#+id/changeEmailButton"
android:background="#drawable/buttonshape"
android:layout_below="#+id/userPhotoImageView"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Password"
android:textSize="25sp"
android:textColor="#0288D1"
android:layout_marginTop="25dp"
android:background="#drawable/buttonshape"
android:id="#+id/changePasswordEmailButton"
android:layout_below="#+id/changeEmailButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Account"
android:textSize="25sp"
android:textColor="#0288D1"
android:layout_marginTop="25dp"
android:id="#+id/deleteAccountButton"
android:layout_below="#+id/changePasswordEmailButton"
android:layout_centerHorizontal="true"
android:background="#drawable/buttonshape" />
Here is the layout for the AlertDialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/userEmailEditText"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Enter Email" />
<EditText
android:id="#+id/userPasswordEditText"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="Enter Password"/>
</LinearLayout>
Any example to show how to correctly Bind the mUserEmailEditText and mUserPasswordEditText
Thank you
When you use bind like that is supposed to be from a existing ID in your activity layout, since you dont have it in your xml it will crash.. Dont bind the EditText and just create them in the AlertDialog, and im use setParams to change their layout
#Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
#Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;
You can not bind the view from the xml which is not inflated as part of Activity's view. So remove #Bind from above line as follows.
EditText mUserEmailEditText;
EditText mUserPasswordEditText;
and inflate them while creating your dialog in createAlertDialog() method. So replace following 2 lines
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));
with
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
mUserEmailEditText = rootView.findViewById(R.id.userEmailEditText);
mUserPasswordEditText = rootView.findViewById(R.id.userPasswordEditText);
builder.setView(rootView);