gpt4 book ai didi

java - Android - 从 fragment 显示 AlertDialog 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 11:39:50 29 4
gpt4 key购买 nike

我正在尝试发送和接收数据,但出现此错误:

FATAL EXCEPTION: main
Process: smaa.smaa, PID: 4051
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:113)
at android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:291)
at smaa.smaa.BackgroundT.onPreExecute(BackgroundT.java:46)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:613)
at android.os.AsyncTask.execute(AsyncTask.java:560)
at smaa.smaa.FirstFragment.onClick(FirstFragment.java:44)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

这是我的 fragment :

public class FirstFragment extends Fragment implements View.OnClickListener {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_first, container, false);
Button btn1 = (Button)v.findViewById(R.id.btn1);
btn1.setOnClickListener(this);
return v;
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
String method = "see";
BackgroundT backgroundT = new BackgroundT(this);
backgroundT.execute(method);
break;
}
}
}

还有我的后台任务:

public class BackgroundT extends AsyncTask<String,Void,String> {
AlertDialog alertDialog;
Context ctx;
String response = "";

private Context applicationContext;
private Context activity;

public BackgroundT(Context ctx) {
this.ctx = ctx;
}

public BackgroundT(FirstFragment firstFragment) {
this.ctx = ctx;
}


@Override
protected void onPreExecute() {
super.onPreExecute();
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Ver Tickets");
}

@Override
protected String doInBackground(String... params) {
String see_url = "https://smaa.000webhostapp.com/androidver.php";

String method = params[0];

if (method.equals("see")) {
String login_name = params[1];
String login_pass = params[2];
String test = "test";
try {
URL url = new URL(see_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);

OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

String data = URLEncoder.encode("test", "UTF-8") + "=" + URLEncoder.encode(test, "UTF-8");

bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();

InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));


String line = "";

while ((line = bufferedReader.readLine()) != null) {
response += line;
}

bufferedReader.close();
inputStream.close();

httpURLConnection.disconnect();

return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {

}
return null;
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx, response, Toast.LENGTH_LONG).show();
}
}

我知道问题出在 Context 或 ctx 中,但我不知道在哪里,我使用过类似的东西并且它以前工作过,所以我不知道现在问题是什么,这是一个工作版本:

后台任务:http://pastebin.com/UnxaSV2X

主要 Activity :http://pastebin.com/rk5jufBc

--- 编辑 ---

感谢@rafsanahmad007,这是我修复的内容:

fragment 代码:

String method = "see";
BackgroundT backgroundT = new BackgroundT(getActivity());
backgroundT.execute(method);
break;

后台任务代码:

public BackgroundT(FragmentActivity activity) {
this.ctx = activity;
}

还有另一个错误,我稍后在这里修复:

String login_name = params[1];
String login_pass = params[2];

这给了我一个错误,因为我没有使用它,谢谢大家。

最佳答案

您需要 Activity 上下文才能在 Fragment 中显示 AlertDialog

而不是;

BackgroundT backgroundT = new BackgroundT(this);
backgroundT.execute(method);

试试这个:

BackgroundT backgroundT = new BackgroundT(getActivity());
backgroundT.execute(method);

编辑

同时编辑构造函数:

public BackgroundT(FragmentActivity activity) {
this.ctx = activity;
}

关于java - Android - 从 fragment 显示 AlertDialog 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42932361/

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