为什么我的TextViews为null? [英] Why are my TextViews null?
问题描述
我想如果用户输入的数字无效,则两个TextView
应该更改其文本.
我抬头检查是否有多个具有相同ID的TextView
,结果是我的TextView
中的每个ID都是唯一的.
I want if the user enters an invalid number, two TextView
s 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 TextView
s are unique.
每次我输入错误的数字来强制我的错误时,我的应用程序就会崩溃.如果我在调试中启动应用程序并标记了我的两个问题代码".结果是两个TextView
均为null
.
我不知道为什么会这样,因为ID是唯一的,而且我不会更改当前的布局.
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 TextView
s were null
.
I don't know why this happens because the id's are unique and I don't change my current layout.
我的Java代码:
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;
}
}
还有我的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"/>
我研究了一下,在stackoverflow.com上发现的唯一解决方案是我需要使用setContentView();
,但与此同时我遇到了两个问题:
1.如果不更改布局,为什么要使用它?
2.我尝试过,但我的应用程序没有崩溃,但我的文本没有更改. (在使用setText();"
之前先使用setContentView();
)
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)
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;
}
其他所有重要内容(4k行代码)都没有重要
Everything else (abput 4k Lines of code) arent important
推荐答案
这些代码行对我而言似乎不正确.他们看起来好像不会做任何事情:
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);
您没有看到任何效果的原因是这些是返回某些值的get语句,但是您实际上并没有使用这些值来做任何事情.
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);
我还建议您在onCreate方法中花一些时间来完成所有findView的工作,这样您就不必一遍又一遍地做. (它也更清晰一些,为您节省了很多样板Java.)
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.)
您可以将各种TextView元素(等)定义为该类的私有成员,因此-例如,仅针对我们已经在讨论的2个TextView:
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);
...
}
完成此操作后,您可以从类中的其他任何地方引用两个TextView,就像它们的成员一样:hKnowledge和tKnowledge;
Having done this, you can then refer to the two TextViews from anywhere else in your class just as their members: hKnowledge and tKnowledge;
这篇关于为什么我的TextViews为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!