一卷旌收千骑虏,万全身出百重围。这篇文章主要讲述设置Textview时的Android java.lang.NullPointerException [重复]相关的知识,希望能为你提供帮助。
这个问题在这里已有答案:
- What is a NullPointerException, and how do I fix it? 12个答案
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 [重复]】你的
noteText
是null
,因为你只是忘了在你的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);
推荐阅读
- Android Studio。 AddRule Null指针异常
- 尝试在空对象引用[duplicate]上调用虚方法'java.io.File android.content.Context.getCacheDir()'
- android.os.Looper对空对象引用的android.content.Context.getMainLooper()
- Android事物(在Raspberry PI 3上通过USB UART接收数据时出现NullPointerException)
- Android NullPointerException(println需要一条消息)
- android(canvas触摸不在s4中工作)
- 如何在Symfony 1.4中为表单禁用CSRF保护/验证
- 如何从Symfony 1.4中的任务(控制台命令)访问数据库(Doctrine连接)
- 低代码与自定义软件(哪个是更好的选择(什么时候?))