为什么我的TextViews为null?

时间:2017-08-10 00:15:57

标签: java android methods widget

我想如果用户输入的号码无效,则两个TextView应该更改其文本 我查了一下,检查了我是否有多个具有相同ID的TextView,并且结果是我的TextView中的每个id都是唯一的。

每次我强制输入无效号码时出错,我的应用程序崩溃了。如果我在调试中启动应用程序并标记我的两个“有问题的代码”。结果是TextView s null。{。} 我不知道为什么会发生这种情况,因为id是唯一的,我不会改变我当前的布局。

我的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();但我有两个问题:
如果我没有选择布局,为什么要使用它呢? 我试过了,我的应用程序没有崩溃,但我的文本没有改变。 (setContentView();之前使用了setText();"

这是我的整个错误报告(没有调试过程):

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代码行)并不重要

2 个答案:

答案 0 :(得分:0)

onCreate(),您必须致电setContentView(R.layout.whateveryourxml)。在那之后,但仍然追溯到你的onCreate,你可以打电话给errorKnowledge()。这应该确保TextView都不为null。

答案 1 :(得分:0)

[编辑:只是为了澄清,你必须在onCreate期间调用setContentView方法,否则你的TextViews将解析为null。假设你已经完成了 - 就像你发布的代码一样 - 请继续阅读!]

这些代码对我来说似乎不对。他们看起来不会做任何事情:

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语句,但实际上你并没有使用这些值来任何事情。

我觉得你真正想说的是:

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。)

您可以将各种TextView元素(等)定义为类的私有成员,因此 - 作为示例,仅适用于我们已经讨论的2个TextView:

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;