我正在使用滑动视图制作登录注销模块 我将类扩展为用于滑动视图的片段,然后我收到了此错误 "构造函数AlertDialog.Builder(Android)未定义" 这种情况发生在我将类扩展到我使用活动之前工作的片段之后
我的整个代码
Android.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.learn2crack.library.DatabaseHandler;
import com.learn2crack.tab.Android;
import java.util.HashMap;
public class Android extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View android = inflater.inflate(R.layout.android_frag, container, false);
DatabaseHandler db = new DatabaseHandler(getActivity());
HashMap<String,String> user = new HashMap<String, String>();
user = db.getUserDetails();
/**
* Displays the registration details in Text view
**/
final ImageButton logoutbtn = (ImageButton)android.findViewById(R.id.logoutbtn);
final TextView fname = (TextView)android.findViewById(R.id.fname);
final TextView lname = (TextView)android.findViewById(R.id.lname);
final TextView uname = (TextView)android.findViewById(R.id.uname);
final TextView email = (TextView)android.findViewById(R.id.email);
final TextView created_at = (TextView)android.findViewById(R.id.regat);
fname.setText(user.get("fname"));
lname.setText(user.get("lname"));
uname.setText(user.get("uname"));
email.setText(user.get("email"));
created_at.setText(user.get("created_at"));
logoutbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
logout();
}
});
return android;}
public void logout(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Android.this);
alertDialog.setTitle("Logout"); // Sets title for your alertbox
alertDialog.setMessage("Are you sure you want to Logout ?"); // Message to be displayed on alertbox
/* When positive (yes/ok) is clicked */
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent login = new Intent(getActivity(), Login.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
getActivity().finish();
}
});
/* When negative (No/cancel) button is clicked*/
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
}
答案 0 :(得分:2)
如果您的父类(Function)有一个构造函数,您的子类也需要有一个,否则请使用Android.this.getActivity());
。
答案 1 :(得分:1)
Fragments
不要从Context
延伸,您需要提供Context
参数。因此,请使用Android.this
。
Android.this.getActivity()