Changing value of TextView from Dialogs - android

I'm trying to make change a TextView 's text by the value submitted from a EditText on a Dialog.
There's one Dialog that makes you Add the amount and on makes you Remove it from the TextView.
What I don't get is why when I click on 'Add' or 'Remove' the app crashes.
Here's the entire Code (Java and XMLs)
http://pastebin.com/er3zTe4V
-- Error --
09-14 20:49:55.661 31774-31774/com.hfad.pocket E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.hfad.pocket, PID: 31774
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.hfad.pocket.MainActivity$1.onClick(MainActivity.java:32)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
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)

Since your EditText is in the dialog layout, you need to call findViewById on your dialog:
EditText transaction = (EditText) ((AlertDialog) dialog).findViewById(R.id.editText);

Related

TextView with clickablespan throwing exception on long click

I've a TextView in my app where every word is a clickable span. Everything works fine until I long click any word, which throws the following exception:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.Editor$SelectionModifierCursorController.getMinTouchOffset()' on a null object reference
at android.widget.Editor.touchPositionIsInSelection(Editor.java:917)
at android.widget.Editor.performLongClick(Editor.java:1018)
at android.widget.TextView.performLongClick(TextView.java:9436)
at android.view.View.performLongClick(View.java:5700)
at android.view.View$CheckForLongPress.run(View.java:22431)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6372)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:791)
I've tried setting android:longClickable="false" on the TextView but in vain. Any idea how it can be fixed?
Thanking in anticipation!

Android App crash because of Null Pointer exception on setting onClick listener in Editor class

I have created a Linear Layout onclick of a button which has an edit text. While typing there is a popup window for suggestions that comes up and the app crashes.
Stack trace:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.widget.Editor$SuggestionsPopupWindow.initContentView(Editor.java:3714)
at android.widget.Editor$PinnedPopupWindow.(Editor.java:3375)
at android.widget.Editor$SuggestionsPopupWindow.(Editor.java:3660)
at android.widget.Editor.replace(Editor.java:423)
at android.widget.Editor$3.run(Editor.java:2340)
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:6682)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
I found some related answers which stated that this happens on using Android PopupWindow class since the suggestions are using a PopupWindow as well. My code does not use PopupWindow as well.
This crash is happening mostly on Samsung Note 5, and Galaxy S6.
Any help would be appreciated!
It's saying that your button (which you used like buttonName.setOnClickListener()) is not defined. Check your find view by id function, id inside of it etc.
I think it is because you set in XML a click, but don't have in your class. A check has this tag and remove:
android:onClick="save"

transfer searchview from actionbar to layout

hey guys i want to transfer my searchview from actionbar to inside of my android layout but when im transfering it ill get crashed. how to fix this problem? thanks for any help.
here is my logcat
04-12 19:57:28.277 25122-25122/com.example.lightning.myapplication3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lightning.myapplication3, PID: 25122
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lightning.myapplication3/com.example.lightning.myapplication3.SetRouteActivity}: java.lang.ClassCastException: android.widget.SearchView cannot be cast to android.support.v7.widget.SearchView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2650)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1505)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
Caused by: java.lang.ClassCastException: android.widget.SearchView cannot be cast to android.support.v7.widget.SearchView
at com.example.lightning.myapplication3.SetRouteActivity.onCreate(SetRouteActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6270)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2537)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2650) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1505) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:207) 
at android.app.ActivityThread.main(ActivityThread.java:5776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749) 
What you receive is a ClassCastException
This means, the type you use is wrong.
searchView = (SearchView)findViewById(R.id.pointa);
You have two choices:
1) Look for the import statement of SearchView at the top of this class file and change it from android.widget.SearchView to android.support.v7.widget.SearchView (or the other way round).
2) Take a close look at your XML file, looking for the <SearchView Tag. Is it possible, that because of the move, it changes from widget to support SearchView?
One of those two will fix your problem.
Hope this helps.

How to programmatically change the Text and Image of the Navigation Header for android

I have looked everywhere and there are only so many people that have asked this question and so far nothing is working. I am currently working on an app that is based on fragments and when someone enters their id, it downloads their name and picture. I want to be able to change their name in the nav header field. Currently this is the code I am using
View header = LayoutInflater.from(getActivity()).inflate(R.layout.nav_header_main, null);
navigationView.addHeaderView(header);
test = (TextView) header.findViewById(R.id.username);
test.setText("HELLO");
That I obtained from here https://code.google.com/p/android/issues/detail?id=190786
I also tried this method here In android how to set navigation drawer header image and name programmatically in class file?
Both of these end up with this error
FATAL EXCEPTION: main
Process: com.horizonservers.horizon, PID: 4042
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.horizonservers.horizon/com.horizonservers.horizon.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.NavigationView.addHeaderView(android.view.View)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void'
android.support.design.widget.NavigationView.addHeaderView(android.view.View)' on a null object reference
at com.horizonservers.horizon.MainFragment.onCreateView(MainFragment.java:153)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
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) 
Does anyone know how to change the Text and image of the nav header side bar? https://gyazo.com/23534130df4aff888708415b368aa1fa
NavigationView navigationView = findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0);
test = (TextView) header.findViewById(R.id.username);
test.setText("HELLO");
Maybe this would help!!
If anyone finds this in the future, I was able to find a library that does pretty much exactly what I needed it to do. You can find it here.
Based on your error, it looks like navigationView is null. Check to make sure navigationView was properly initialized BEFORE you inflate the header. Depending on how/where you're using fragments, the fragment lifecycle could be initializing the navigationView after the header inflation which will throw a NullPointerException.
If not the above, navigation header inflation has become very sensitive to the android environment used with recent design changes to the google design library. Below was my all-inclusive solution, and maybe it will work for you.
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View header = navigationView.inflateHeaderView(R.layout.nav_header_main);
test = (TextView) header.findViewById(R.id.username);
test.setText("HELLO");

Android Weird Null Object Reference on Button.setText(mText)

I'm stuck with the weirdest error I've ever got since I started programming. I'm setting a text to a button. Nothing complicated there!
I do it this way:
btnButton.setText(cell.getText());
btnButton exists since I've passed some others things to it before setting its text and cell.getText() is not null either since I can log the text in my console.
Still, I get this weird error:
03-31 14:58:35.407 24398-24398/ca.gggolf.agggolf.full E/AndroidRuntime: FATAL EXCEPTION: main
Process: ca.gggolf.agggolf.full, PID: 24398
java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
at android.widget.TextView.checkForRelayout(TextView.java:6836)
at android.widget.TextView.setText(TextView.java:4063)
at android.widget.TextView.setText(TextView.java:3921)
at android.widget.TextView.setText(TextView.java:3896)
at ca.gggolf.agggolf.activities.fragments.AutoGridFragment.displayAutoGridBody(AutoGridFragment.java:467)
at ca.gggolf.agggolf.activities.fragments.AutoGridFragment.createAutogridBody(AutoGridFragment.java:206)
at ca.gggolf.agggolf.activities.fragments.AutoGridFragment.onCreate(AutoGridFragment.java:94)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1939)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1029)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
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)
Can someone help me fix this? It sometimes work but it depends from where in the application that I come from.
Thanks!
EDIT
Here's my whole code when creating the button. I assign it's width and initial text and then if the text should be truncated, I measure to make sure that my button will still fit in the size I gave it otherwise, I change it to textView.
It's when I set it back to normal after that it gives me the error.
Button btnButton = new Button(getActivity());
btnButton.setWidth(width);
btnButton.setText(cell.getText());
if(adjustedAutogrid.getColumn(indexCell).isTruncated()){
btnButton.setText("…");
btnButton.measure(0, 0);
if(btnButton.getMeasuredWidth() > width){
TextView btnText = new TextView(getActivity());
btnText.setWidth(width);
btnText.setText(cell.getText());
cell.getCSS().applyToTextView(btnText);
btnText.setEllipsize(TextUtils.TruncateAt.END);
btnText.setLines(1);
layout.addView(btnText);
break;
}
btnButton.setText(cell.getText());
btnButton.setEllipsize(TextUtils.TruncateAt.END);
btnButton.setLines(1);
}
And for those wondering from where it causes problem, well it's a bit complicated. I receive information through internet (an XML) that I convert to a table that I show. I must set the different cells the appropriate size and content. When I load what we call an AutoGrid coming from the XML, all my AutoGrid works except for one...
Edit 2
Everything works fine if I leave the text to the ellipsize symbol (…). It really is when i try to set the text again that it doesn't work..
I don't know why but it seems that if I set the text later with my button that everything is fine...
btnButton.setWidth(width);
btnButton.setEllipsize(TextUtils.TruncateAt.END);
btnButton.setLines(1);
btnButton.setText(cell.getText()); // this line used to be the first one
If someone has an explanation, please fill free to enlighten me!

Categories

Resources