嗨,我是Android开发的初学者,所以我真的不太了解我在做什么。我一直得到nullpointerexecption,并且每当调用getActivity()时我的程序都会崩溃。这包括Toast消息以及当我尝试以意图启动另一个活动时。我不完全确定如何使用意图,如果可以解释那将是很好的。
以下是具有getActivity()调用以及intent的类:
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
public class loginDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.loginw)
.setItems(R.array.account_types, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
if(which==0){
Toast.makeText(getActivity(),"todo",Toast.LENGTH_SHORT).show();
}else if(which==1){
Toast.makeText(getActivity(),"todo",Toast.LENGTH_SHORT).show();
}else if(which==2){
AlertDialog.Builder builder1=new AlertDialog.Builder(getActivity());
LayoutInflater inflater=getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.login_dialog,null);
builder1.setView(view);
builder1.setTitle("Login with Email Address");
builder1.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog1, int which) {
//Toast.makeText(getActivity(), "Cancel", Toast.LENGTH_SHORT).show(); //Force Closes. Nullpointer Exception? Probably because of getActivity()
//dialog1.dismiss();
}
});
builder1.setNeutralButton("Forgot Password",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog1, int which){
dialog1.dismiss();
AlertDialog.Builder builder2=new AlertDialog.Builder(getActivity());
builder2.setTitle("Forgot Password");
builder2.setMessage("Enter your Email Address");
builder2.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//TODO
}
});
builder2.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//TODO
}
});
builder2.show();
}
});
builder1.setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog1, int which) {
//Toast.makeText(getActivity(),"Login complete",Toast.LENGTH_SHORT).show(); //Force Closes. Nullpointer Exception? Probably because of getActivity()
dialog1.dismiss();
Intent intent = new Intent(getActivity(),Groups.class);
startActivity(intent);
}
});
Dialog dialog1 = builder1.create();
dialog1.show();
}
}
});
Dialog dialog=builder.create();
return dialog;
}
}
这是logcat(请注意,这仅适用于intent调用,因为其他getActivity()被注释掉了):
java.lang.NullPointerException
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:3813)
at com.app.u.loginDialog$1$3.onClick(loginDialog.java:67)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
在Overriden dialogfragment类中返回onActivityCreated方法后,应该调用getActivity。在你的案例loginDialog类。这是调用getActivity的安全的地方
public class LoginDialog extends DialogFragment {
Activity mAct;
@Override
public void onAttach(Activity act) {
super.onAttach(act);
mAct = act; // use this mAct instead of getActivity() function
}
答案 1 :(得分:1)
你不能在对话弹出消息中创建Intent所以你应该像这样使用或在你的对话中声明这样它会帮助你绝对......一切顺利
Context context = activity.getApplicationContext();
Intent intent = new Intent(context, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
答案 2 :(得分:0)
尝试使用getApplicationContext()
代替
Intent intent = new Intent(getApplicationContext(), Groups.class);
startActivity(intent);
请记得在AndroidManifest文件中声明您的新活动!
<activity android:name=".Groups"/>