问题
I want if the user enters an invalid number, two TextViews should change their texts.
I looked up and checked if I got more than one TextView with the same id and the ruesult was that each id from my TextViews are unique.
Everytime I force my error with entering an invalid number my app crashes. If i start the app in debug and marked my two "problematic codes". The result was both TextViews were null.
I don't know why this happens because the id's are unique and I don't change my current layout.
My java code:
public void checkingInput(View view) {
TextView eKnowledge = (TextView) findViewById(R.id.enterKnowledge);
try{
int inputNumber = Integer.parseInt(eKnowledge.getText().toString());
_knowledge = KnowledgeLevel.fromUserInput(inputNumber);
test();
hideKeyboard(view);
} catch(NumberFormatException e){
errorKnowledge();
hideKeyboard(view);
}
}
@SuppressLint("SetTextI18n")
public void test() {
setContentView(R.layout.test);
TextView test = (TextView) findViewById(R.id.textView_nur_einmal_benutzt_hoff_ich);
switch(_knowledge) {
case NOOB:
test.setText("ENUM = NOOB");
break;
case BEGINNER:
test.setText("ENUM = BEGINNER");
break;
case ADVANCED:
test.setText("ENUM = ADV");
break;
case PRO:
test.setText("ENUM = PRO");
break;
case GRANDMASTER:
test.setText("ENUM = GM");
break;
case ERROR:
errorKnowledge();
break;
case FAIL:
test.setText("FEHLER!");
break;
}
}
@SuppressLint("SetTextI18n")
public void errorKnowledge(){
TextView hKnowledge = (TextView) findViewById(R.id.testWissenstandHeader);
TextView tKnowledge = (TextView) findViewById(R.id.testWissenstandText);
/* da ist der fehler ... einmal mit dem debugger durch zeigt es doch direkt: hKnowledge und tKnowledge sind null, daher die NullPointerException */
hKnowledge.getResources().getString(R.string.FehlerGefunden);
hKnowledge.getResources().getColor(android.R.color.holo_red_dark);
tKnowledge.getResources().getString(R.string.FehlerText1);
tKnowledge.getResources().getColor(android.R.color.holo_red_light);
}
private enum KnowledgeLevel {
FAIL, ERROR, NOOB, BEGINNER, ADVANCED, PRO, GRANDMASTER;
static KnowledgeLevel fromUserInput(final int input)
{
if (input >= 11 || input <= -1) {
return ERROR;
}
else if (input == 10) {
return GRANDMASTER;
}
else if (input >= 7) {
return PRO;
}
else if (input >= 4) {
return ADVANCED;
}
else if (input >= 1 ) {
return BEGINNER;
}
else if (input == 0) {
return NOOB;
}
return FAIL;
}
}
And my XML:
<TextView
android:id="@+id/testWissenstandHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="Bla"
android:textAlignment="center"
android:textColor="#511613"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/kastenKnowledge"
app:layout_constraintLeft_toLeftOf="@+id/kastenKnowledge"
app:layout_constraintRight_toRightOf="@+id/kastenKnowledge"
app:layout_constraintTop_toTopOf="@+id/kastenKnowledge"/>
<TextView
android:id="@+id/testWissenstandText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text='BlaBla'
android:textAlignment="center"
android:textColor="#E0E0E0"
android:textSize="20sp"
app:layout_constraintHorizontal_bias="0.512"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/testWissenstandHeader"/>
I researched a bit and the only solution I found on stackoverflow.com was that I need to use setContentView(); but I've got two problems with that:
1. Why should I use that if I don't chnage my layout?
2. I tried it and my app didn't crash BUT my texts didn't change. (setContentView(); were used before setText();" is used)
Here's my whole errorreport (no debug prozess):
08-10 00:14:53.580 2774-2774/com.example.alexander.hashtagnevereverbuggy E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alexander.hashtagnevereverbuggy, PID: 2774
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:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.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:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.widget.TextView.getResources()' on a null object reference
at com.example.alexander.hashtagnevereverbuggy.MainActivity.errorKnowledge(MainActivity.java:112)
at com.example.alexander.hashtagnevereverbuggy.MainActivity.test(MainActivity.java:97)
at com.example.alexander.hashtagnevereverbuggy.MainActivity.checkingInput(MainActivity.java:68)
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:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
EDIT:
package com.example.alexander.hashtagnevereverbuggy;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import java.util.Map;
@SuppressWarnings("deprecation")
public class MainActivity extends AppCompatActivity implements Heroes {
private KnowledgeLevel _knowledge = KnowledgeLevel.NOOB;
private static FragmentManager _fragmentManager;
private enum WerteTextFelder {
TITLE,TYP, DAMAGE, LADUNG, KUGELN, NACHLADEDAUER, HEILUNG, HEADSHOT, WIRKUNGSDAUER, EXTRAEFFEKT, BESONDERHEIT
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, getString(R.string.AppID));
_fragmentManager = getFragmentManager();
Button testButtonAnfang = (Button) findViewById(R.id.AnfangsButton);
testButtonAnfang.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
setContentView(R.layout.testwissenstand);
}
});
}
@Override
public void onBackPressed() {
if(getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
}
super.onBackPressed();
}
public void checkingInput(View view) {
TextView eKnowledge = (TextView) findViewById(R.id.enterKnowledge);
try{
int inputNumber = Integer.parseInt(eKnowledge.getText().toString());
_knowledge = KnowledgeLevel.fromUserInput(inputNumber);
test();
hideKeyboard(view);
} catch(NumberFormatException e){
errorKnowledge();
hideKeyboard(view);
}
}
@SuppressLint("SetTextI18n")
public void test() {
setContentView(R.layout.test);
TextView test = (TextView) findViewById(R.id.textView_nur_einmal_benutzt_hoff_ich);
switch(_knowledge) {
case NOOB:
test.setText("ENUM = NOOB");
break;
case BEGINNER:
test.setText("ENUM = BEGINNER");
break;
case ADVANCED:
test.setText("ENUM = ADV");
break;
case PRO:
test.setText("ENUM = PRO");
break;
case GRANDMASTER:
test.setText("ENUM = GM");
break;
case ERROR:
errorKnowledge();
break;
case FAIL:
test.setText("FEHLER!");
break;
}
}
@SuppressLint("SetTextI18n")
public void errorKnowledge(){
TextView hKnowledge = (TextView) findViewById(R.id.testWissenstandHeader);
TextView tKnowledge = (TextView) findViewById(R.id.testWissenstandText);
/* da ist der fehler ... einmal mit dem debugger durch zeigt es doch direkt: hKnowledge und tKnowledge sind null, daher die NullPointerException */
hKnowledge.getResources().getString(R.string.FehlerGefunden);
hKnowledge.getResources().getColor(android.R.color.holo_red_dark);
tKnowledge.getResources().getString(R.string.FehlerText1);
tKnowledge.getResources().getColor(android.R.color.holo_red_light);
}
private enum KnowledgeLevel {
FAIL, ERROR, NOOB, BEGINNER, ADVANCED, PRO, GRANDMASTER;
static KnowledgeLevel fromUserInput(final int input)
{
if (input >= 11 || input <= -1) {
return ERROR;
}
else if (input == 10) {
return GRANDMASTER;
}
else if (input >= 7) {
return PRO;
}
else if (input >= 4) {
return ADVANCED;
}
else if (input >= 1 ) {
return BEGINNER;
}
else if (input == 0) {
return NOOB;
}
return FAIL;
}
}
public static void addTransaction(Fragment fragment, String tag){
if (_fragmentManager != null) {
FragmentTransaction fragmentTransaction = _fragmentManager.beginTransaction();
fragmentTransaction.add(fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();
}
}
private void hideKeyboard(View view){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
private class geileListener implements View.OnClickListener{
private Hero hero;
private geileListener (Hero hero) {
this.hero = hero;
}
@Override
public void onClick(View view) {
setContentView(hero.getHeroLayout());
}
}
private void advertisment() {
/*Notwendig um advertisment im showlock darzustellen*/
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
public void showPreview (Hero hero) {
setContentView(hero.getPreviewLayout());
ImageButton button = (ImageButton) findViewById(R.id.imageButton_Preview);
button.setImageResource(hero.getPreviewImage());
button.setOnClickListener(new geileListener(hero));
advertisment();
}
private Map<WerteTextFelder, TextView> getWerteTextFelder() {
setContentView(R.layout.werte);
Map<WerteTextFelder, TextView> map = new HashMap<>();
TextView Titel = (TextView) findViewById(R.id.textView_Überschrift);
TextView Typ = (TextView) findViewById(R.id.textView_Werte_0);
TextView Damage = (TextView) findViewById(R.id.textView_Wert_1);
TextView Ladung = (TextView) findViewById(R.id.textView_Wert_2);
TextView Kugeln = (TextView) findViewById(R.id.textView_Wert_3);
TextView Nachladedauer = (TextView) findViewById(R.id.textView_Wert_4);
TextView Heilung = (TextView) findViewById(R.id.textView_Wert_5);
TextView Headshot = (TextView) findViewById(R.id.textView_Wert_6);
TextView Wirkungsdauer = (TextView) findViewById(R.id.textView_Wert_7);
TextView ExtraEffekt = (TextView) findViewById(R.id.textView_Wert_8);
TextView wert9 = (TextView) findViewById(R.id.textView_Wert_9);
map.put(WerteTextFelder.TITLE, Titel);
map.put(WerteTextFelder.TYP, Typ);
map.put(WerteTextFelder.DAMAGE, Damage);
map.put(WerteTextFelder.LADUNG, Ladung);
map.put(WerteTextFelder.KUGELN, Kugeln);
map.put(WerteTextFelder.NACHLADEDAUER, Nachladedauer);
map.put(WerteTextFelder.HEILUNG, Heilung);
map.put(WerteTextFelder.HEADSHOT, Headshot);
map.put(WerteTextFelder.WIRKUNGSDAUER, Wirkungsdauer);
map.put(WerteTextFelder.EXTRAEFFEKT, ExtraEffekt);
map.put(WerteTextFelder.BESONDERHEIT, wert9);
return map;
}
Everything else (abput 4k Lines of code) arent important
回答1:
In your onCreate(), you have to to call setContentView(R.layout.whateveryourxml). After that, but still tracing back to your onCreate, you may call errorKnowledge(). This should make sure that neither TextView is null.
回答2:
[Edit: Just to clarify, you do have to call the setContentView method during onCreate, or your TextViews will resolve to null. Assuming you've done that - as in the code you posted - read on!]
These lines of code don't seem right to me. They don't look as if they will do anything:
hKnowledge.getResources().getString(R.string.FehlerGefunden);
hKnowledge.getResources().getColor(android.R.color.holo_red_dark);
tKnowledge.getResources().getString(R.string.FehlerText1);
tKnowledge.getResources().getColor(android.R.color.holo_red_light);
The reason you're seeing no effect is that these are get statements that return some values, but then you don't actually use those values to do anything.
I feel like what you actually want to say is this:
hKnowledge.setText(R.string.FehlerGefunden);
hKnowledge.setTextColor(android.R.color.holo_red_dark);
tKnowledge.setText(R.string.FehlerText1);
tKnowledge.setTextColor(android.R.color.holo_red_dark);
I'd also recommend that you spend some time in your onCreate method doing all the findView work, so that you don't need to do it again and again later. (It's also a little clearer and saves you quite a bit of boilerplate java.)
You can define your various TextView elements (etc.) as private members for the class, so - as an example, just for the 2 TextViews we're already talking about:
private TextView hKnowledge;
private TextView tKnowledge;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hKnowledge = (TextView) findViewById(R.id.testWissenstandHeader);
tKnowledge = (TextView) findViewById(R.id.testWissenstandText);
...
}
Having done this, you can then refer to the two TextViews from anywhere else in your class just as their members: hKnowledge and tKnowledge;
来源:https://stackoverflow.com/questions/45602527/why-are-my-textviews-null