[英]How to call method of the another class?
我的AddImages.java
類中有一個showAlert
方法,下面是它的實現
//show Alert box
public void showAlert(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(title);
builder.setMessage(message);
builder.setNeutralButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
我想在我的AddCateogory.java文件中使用此方法,所以我通過使用new運算符(如下所示)來調用該方法
package com.example.lenovo.jdstudio;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class AddCategory extends Fragment {
EditText mCategory;
Button mSbtButton;
public AddCategory() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_add_category, container, false);
//initialise variable
mCategory = (EditText) v.findViewById(R.id.edit_add_category);
mSbtButton = (Button) v.findViewById(R.id.category_submit_button);
//set clickEvent on the submit button
mSbtButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isValid())
{
startAdding();
}
else{
//Method from the AddImages.java
new AddImages().showAlert("Error", "Please fill all the details");
}
}
});
return v;
}
// Add categoty value to the database
private void startAdding() {
}
// checks the field is empty or not
private boolean isValid() {
String category_value = mCategory.getText().toString().trim();
if(!TextUtils.isEmpty(category_value)){
return true;
}
return false;
}
}
但這給了我以下錯誤
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lenovo.jdstudio, PID: 20537
java.lang.NullPointerException
at android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:114)
at android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:294)
at com.example.lenovo.jdstudio.AddImages.showAlert(AddImages.java:168)
at com.example.lenovo.jdstudio.AddCategory$1.onClick(AddCategory.java:47)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Disconnected from the target VM, address: 'localhost:8604', transport: 'socket'
因為我已經在擴展Fragment類,所以我不能擴展AddImages.java
。 那么執行此任務的正確方法是什么。請幫助我擺脫此問題
嘗試傳遞Context
並使您的方法static
public static void showAlert(Context context, String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
builder.setNeutralButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
如果您在父活動中使用showAlert()
方法,則showAlert()
像這樣調用
(YouActivity)getActivity()).showAlert((getActivity(), "Title", "Message"); // pass the context
如果您的showAlert()
在其他類之外使用此方法
showAlert((getActivity(), "Title", "Message"); // pass the context
((MainActivity)getActivity()).showAlert();
不要使用static,這是一個非常糟糕的做法,而不是在上面的代碼中使用此方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.