无法在onCreate()事件Android

时间:2016-03-15 18:14:02

标签: android

我通常在应用程序启动时在onCreate()事件中初始化。 。 但是当我把我的代码放在onCreate()事件中时:

preferenceManager = CallInfoPreferenceManager.getInstance();
recorder = new AudioRecorder(preferenceManager.getStartDate());

初始化对象但返回preferenceManager = nullrecorder = null

我在使用参数Bunble savedInstanceState时会遇到问题。

这里我的代码是这样的:

private SharedPreferences sharedPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fileupload);
    ButterKnife.bind(this);

    // Initialize at here

    preferenceManager = CallInfoPreferenceManager.getInstance();
    recorder = new AudioRecorder(preferenceManager.getStartDate());
}

我不知道我必须添加更多代码才能了解更多信息?

更新:

了解更多信息: 这是UploadRecord类,我想初始化AudioRecord以在这个类中使用。

public class UploadRecord extends AppCompatActivity {
    CallInfoPreferenceManager preferenceManager;

    AudioRecorder recorder;
    private SharedPreferences sharedPref;

    private final UploadServiceBroadcastReceiver uploadReceiver =
            new UploadServiceBroadcastReceiver() {

                @Override
                public void onProgress(String uploadId, int progress) {
                    //any code
                }

                @Override
                public void onError(String uploadId, Exception exception) {
                    //any code
                }

                @Override
                public void onCompleted(String uploadId, int serverResponseCode, byte[] serverResponseBody) {
                    //any code
                }

                @Override
                public void onCancelled(String uploadId) {
                    //any code
                }
            };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fileupload);
        ButterKnife.bind(this);

        // Initialize at here

        preferenceManager = CallInfoPreferenceManager.getInstance();
        recorder = new AudioRecorder(preferenceManager.getStartDate());
    }

    @Override
    protected void onResume() {
        super.onResume();
        uploadReceiver.register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        uploadReceiver.unregister(this);
    }
}

我的班级AudioRecorder是:

public class AudioRecorder {

    static String fileName;

    MediaRecorder mediaRecorder;
    private String a;
    ConfigPreferenceManager configPreferenceManager;

    public AudioRecorder(String startDate) {
        configPreferenceManager = ConfigPreferenceManager.getInstance();
        this.fileName = courseGet(startDate);
        mediaRecorder = new MediaRecorder();
    }

    public AudioRecorder() {
        configPreferenceManager = ConfigPreferenceManager.getInstance();
    }
}

在Log cat视图中显示异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String kr.co.composer.callrecord.recorder.AudioRecorder.getFileName()' on a null object reference
System.err:     at kr.co.composer.callrecord.recorder.UploadRecord.MultipartUploadForRecordApp(UploadRecord.java:186)

在文件UploadRecord.java的第186行,我调用class来初始化AudioRecord()

我调试它显示值为AudioRecord = null

0 个答案:

没有答案