几天前我有一个正常运行的程序,我现在换了几个按钮,现在出现了这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jack.synthesiser, PID: 10903
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jack.synthesiser/com.example.jack.synthesiser.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.jack.synthesiser.MainActivity.initializeView(MainActivity.java:59)
at com.example.jack.synthesiser.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
这可能是一个简单的错误,但似乎找不到它的修复,任何帮助将非常感激。继承我的XML。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jack.synthesiser.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint ="Enter frequency"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="218dp"
android:id="@+id/editText" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/radioGroup">
<RadioButton
android:text="Sine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:id="@+id/radioButton" />
<RadioButton
android:text="Square"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton2"
android:layout_above="@+id/toggleButton"
android:layout_toEndOf="@+id/radioButton"
android:layout_marginStart="15dp" />
<RadioButton
android:text="Saw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton3"
android:layout_marginStart="15dp"
android:layout_alignBaseline="@+id/radioButton2"
android:layout_alignBottom="@+id/radioButton2"
android:layout_toEndOf="@+id/radioButton2" />
</RadioGroup>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginStart="55dp"
android:layout_below="@+id/radioGroup"
android:layout_alignStart="@+id/editText"
android:layout_marginTop="44dp" />
<SeekBar
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="@+id/Volume"
android:layout_marginStart="52dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/button" />
<TextView
android:text="@string/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_alignTop="@+id/button"
android:layout_alignStart="@+id/Volume" />
</RelativeLayout>
这是主要的活动文件:
package com.example.jack.synthesiser;
import android.content.Context;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener, SeekBar.OnSeekBarChangeListener {
EditText frequencyInput;
TextView displayFrequency;
Button startStop;
RadioButton sine;
RadioButton square;
RadioButton saw;
PlaySine sineWave = new PlaySine();
PlaySquare squareWave = new PlaySquare();
PlaySaw sawWave = new PlaySaw();
//Filter attack = new Filter();
SeekBar amplitude;
AudioManager audioManager;
float curVolume, maxVolume, volume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeView();
// volumeSounds();
}
//protected void volumeSounds() {
// audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
// curVolume = (float)audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
// maxVolume = (float)audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// volume = curVolume/maxVolume;
//}
private void initializeView () {
frequencyInput = (EditText) findViewById(R.id.editText);
startStop = (Button) findViewById(R.id.toggleButton);
sine = (RadioButton) findViewById(R.id.radioButton);
square = (RadioButton) findViewById(R.id.radioButton2);
saw = (RadioButton) findViewById(R.id.radioButton3);
//startStopSquare = (ToggleButton) findViewById(R.id.toggleButton2);
//startStopSaw = (ToggleButton) findViewById(R.id.toggleButton3);
displayFrequency = (TextView) findViewById(R.id.textView);
startStop.setOnTouchListener(this);
amplitude = (SeekBar) findViewById(R.id.Volume);
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
amplitude.setOnSeekBarChangeListener(this);
//startStopSquare.setOnClickListener(this);
//startStopSaw.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int frequency = Integer.parseInt(frequencyInput.getText().toString());
displayFrequency.setText(String.valueOf(frequency));
sineWave.setSine(frequency);
squareWave.setSquareWave(frequency);
sawWave.setSawWave(frequency);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean on = ((startStop.isPressed()) && sine.isChecked());
boolean sqOn = ((startStop.isPressed()) && square.isChecked());
boolean sawOn =((startStop.isPressed()) && saw.isChecked());
if (on) {
sineWave.start();
} else if (!on){
sineWave.stop();
}
if (sqOn) {
squareWave.start();
} else if (!sqOn) {
squareWave.stop();
}
if (sawOn) {
sawWave.start();
} else if (!sawOn) {
sawWave.stop();
}
return false;
}
}
答案 0 :(得分:0)
您收到错误,因为当您尝试在其中一个按钮上调用.setOnClickListener()
时,变量为null
。例如,请确保首先使用findViewById()
初始化变量。