我一直在关注我发现的一个例子,当做最简单的事情时,在Textview中设置文本它会给我一个错误,我将在下面显示。最常见的问题是在视图膨胀之前分配了文本视图,但这不是这里的情况并且正在改变,尝试不同的布局也不起作用。我相信我在这里有一件简单的事情,但有人可以告诉我它是什么。
#loader {
height: 16px;
width: 16px;
position: fixed;
z-index:1;
left: 50%;
top: 50%;
margin: -25px 0 0 -25px;
background-image: url('../../assets/images/icons/loader.gif');
}
错误代码:
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");
}
}
最后是XML:
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)
非常欢迎任何帮助。
抱歉......我真的很蠢,并没有注意到有重复..谢谢大家的答案
答案 0 :(得分:1)
您的 noteText
是null
,因为您只是忘记在 findViewById
{{{{}} noteText
1}}检查
使用此
TextView
此内容
pitchText = (TextView) findViewById(R.id.pitchText);
noteText = (TextView) findViewById(R.id.noteText);
答案 1 :(得分:1)
pitchText = (TextView) findViewById(R.id.pitchText);
pitchText = (TextView) findViewById(R.id.noteText);
您的noteText
为空,因为您没有分配它。
答案 2 :(得分:1)
你必须改变这个:
pitchText = (TextView) findViewById(R.id.pitchText);
pitchText = (TextView) findViewById(R.id.noteText);
如下:
pitchText = (TextView) findViewById(R.id.pitchText);
noteText= (TextView) findViewById(R.id.noteText);