设置Textview时的Android java.lang.NullPointerException [重复]

一卷旌收千骑虏,万全身出百重围。这篇文章主要讲述设置Textview时的Android java.lang.NullPointerException [重复]相关的知识,希望能为你提供帮助。
这个问题在这里已有答案:

  • What is a NullPointerException, and how do I fix it? 12个答案
我一直在关注我发现的一个例子,当做最简单的事情时,在Textview中设置文本它会给我一个错误,我将在下面显示。最常见的问题是在视图膨胀之前分配了textview,但这不是这种情况并且更改,尝试不同的布局也不起作用。我相信我在这里有一件简单的事情,但有人能告诉我它是什么。
public class MainActivity extends AppCompatActivity {TextView pitchText, noteText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0); pitchText = (TextView) findViewById(R.id.pitchText); pitchText = (TextView) findViewById(R.id.noteText); PitchDetectionHandler pdh = new PitchDetectionHandler() { @Override public void handlePitch(PitchDetectionResult res, AudioEvent e){ final float pitchInHz = res.getPitch(); runOnUiThread(new Runnable() { @Override public void run() { processPitch(pitchInHz); } }); } }; AudioProcessor pitchProcessor = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh); dispatcher.addAudioProcessor(pitchProcessor); Thread audioThread = new Thread(dispatcher, "Audio Thread"); audioThread.start(); }public void processPitch(float pitchInHz) {pitchText.setText("" + pitchInHz); if(pitchInHz > = 110 & & pitchInHz < 123.47) { //A noteText.setText("A"); } else if(pitchInHz > = 123.47 & & pitchInHz < 130.81) { //B noteText.setText("B"); } else if(pitchInHz > = 130.81 & & pitchInHz < 146.83) { //C noteText.setText("C"); } else if(pitchInHz > = 146.83 & & pitchInHz < 164.81) { //D noteText.setText("D"); } else if(pitchInHz > = 164.81 & & pitchInHz < = 174.61) { //E noteText.setText("E"); } else if(pitchInHz > = 174.61 & & pitchInHz < 185) { //F noteText.setText("F"); } else if(pitchInHz > = 185 & & pitchInHz < 196) { //G noteText.setText("G"); } }

错误代码:
03-05 15:12:38.297 7642-7642/com.example.richard.jtransformtest E/androidRuntime: FATAL EXCEPTION: main Process: com.example.richard.jtransformtest, PID: 7642 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.richard.jtransformtest.MainActivity.processPitch(MainActivity.java:62) at com.example.richard.jtransformtest.MainActivity$1$1.run(MainActivity.java:43) at android.os.Handler.handleCallback(Handler.java:836) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6251) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

最后是XML:
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:orientation="vertical" > < TextView android:id="@+id/pitchText" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> < TextView android:id="@+id/noteText" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> < /LinearLayout>

任何帮助都会非常受欢迎。
对不起......我真的很蠢,并没有注意到有重复..谢谢大家的答案
答案【设置Textview时的Android java.lang.NullPointerException [重复]】你的noteTextnull,因为你只是忘了在你的findViewById noteText做正确的TextView检查它
用这个
pitchText = (TextView) findViewById(R.id.pitchText); noteText = (TextView) findViewById(R.id.noteText);

而不是这个
pitchText = (TextView) findViewById(R.id.pitchText); pitchText = (TextView) findViewById(R.id.noteText);

另一答案
pitchText = (TextView) findViewById(R.id.pitchText); pitchText = (TextView) findViewById(R.id.noteText);

你的noteText是空的,因为你没有分配它。
另一答案你必须改变这个:
pitchText = (TextView) findViewById(R.id.pitchText); pitchText = (TextView) findViewById(R.id.noteText);

如下:
pitchText = (TextView) findViewById(R.id.pitchText); noteText= (TextView) findViewById(R.id.noteText);


    推荐阅读