gpt4 book ai didi

java.lang.NullPointerException 在模拟器和手机中停止我的应用程序

转载 作者:行者123 更新时间:2023-12-02 09:14:33 25 4
gpt4 key购买 nike

我在网上搜索过,但找不到合适的答案。该应用程序已安装在手机和模拟器中,但无法打开。该应用程序立即停止并告知将其关闭。但不使用 Ringtone ring =RingtoneManager.getRingtone(getApplicationContext(),RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)); 和相关代码...应用程序运行完美。但我不能使用铃声。如果有任何解决方案或替代方案,请通知我。

所以请帮忙。我的代码如下..

package com.example.analarm;

import androidx.appcompat.app.AppCompatActivity;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.widget.TextClock;
import android.widget.TimePicker;
import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends AppCompatActivity {

TimePicker alarmtime;
TextClock time;

Ringtone ring =RingtoneManager.getRingtone(getApplicationContext(),RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

alarmtime=findViewById(R.id.timepicker);
time=findViewById(R.id.textclock);
Timer t= new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if(time.getText().toString().equals(Alarmtime()))
{
ring.play();
}
else
{
ring.stop();
}

}
},0,1000);
}
public String Alarmtime()
{
Integer alarmHours=alarmtime.getCurrentHour();
Integer alarmMin=alarmtime.getCurrentMinute();
String stringalarmmin;
if(alarmMin<10)
{
stringalarmmin="0";
stringalarmmin=stringalarmmin.concat(alarmMin.toString());
}
else{
stringalarmmin=alarmMin.toString();
}
String stringalarmtime;
if(alarmHours>12)
{
alarmHours= alarmHours-12;
stringalarmtime=alarmHours.toString().concat(":").concat(stringalarmmin).concat(" PM");
}
else
{
stringalarmtime=alarmHours.toString().concat(":").concat(stringalarmmin).concat(" AM");
}
return stringalarmtime;
}
}

我的错误 logcat 在这里...

2019-11-29 00:22:38.375 6352-6352/com.example.analarm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.analarm, PID: 6352
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.analarm/com.example.analarm.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
at com.example.analarm.MainActivity.<init>(MainActivity.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

最佳答案

应在调用 onCreate() 方法后调用 getApplicationContext()。

尝试

Ringtone ring;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ring =RingtoneManager.getRingtone(getApplicationContext(),RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
alarmtime=findViewById(R.id.timepicker);
time=findViewById(R.id.textclock);
Timer t= new Timer();

关于java.lang.NullPointerException 在模拟器和手机中停止我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59104306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com