gpt4 book ai didi

java - 我的界面触发时得到 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 18:35:23 26 4
gpt4 key购买 nike

我尝试了 Toast 消息界面。如果应用程序未连接互联网,我想显示一条Toast消息并且我想要java接口(interface)。

这是MotherActivity.java。此文件实现 ToastMessagges.ToastMessaggeCallback

   public class MotherActivity extends ActionBarActivity implements ToastMessagges.ToastMessaggeCallback {

ToastMessagges toastMessagges;

@Override
protected void onStart() {
super.onStart();

}

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

toastMessagges = new ToastMessagges();

AppStarter();
}

private void AppStarter(){

boolean checkinternet = InternetControl.checkInternetConnection( getApplicationContext() );
if( checkinternet ) {
toastMessagges.show_toast_messagge();
}
else {

}

}

@Override
public void LongToastMessagge() {
Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_LONG).show();


}
}

这是我的 ToastMessagges.java 文件。

public class ToastMessagges {

ToastMessaggeCallback toastMessaggeCallback;
public void show_toast_messagge(){
toastMessaggeCallback.LongToastMessagge();
}


public static interface ToastMessaggeCallback {
public void LongToastMessagge();
}
}

当启动这个应用程序时。我收到 NullPointerException 错误。

Caused by: java.lang.NullPointerException
at com.medyasef.bulenttirasnewapp.bulenttiras.functions.ToastMessagges.show_toast_messagge(ToastMessagges.java:22)
at com.medyasef.bulenttirasnewapp.bulenttiras.MotherActivity.AppStarter(MotherActivity.java:36)
at com.medyasef.bulenttirasnewapp.bulenttiras.MotherActivity.onCreate(MotherActivity.java:29)

ToastMessagges.java:22

toastMessaggeCallback.LongToastMessagge();

抱歉英语不好。请帮忙。谢谢。

最佳答案

您尚未初始化ToastMessaggeCallback toastMessaggeCallback

为此,请编写

ToastMessaggeCallback toastMessaggeCallback = new ToastMessaggeCallback(){

public void LongToastMessagge(){
// add some toasting code here
}

};

这将创建一个实现您的接口(interface)的对象(称为“匿名类”)。当然,您的 ToastMessaggeCallback 应该在方法 LongToastMessagge 中执行某些操作,因此请在其中添加所需的代码。

关于java - 我的界面触发时得到 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22226315/

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