Android fragments 点击发送按钮时应用程序崩溃?
我想将编辑文本中的数据发布到数据库/php脚本,并在列表视图中以编辑文本的相同片段自动显示该数据,但当单击“发送”按钮时,我的应用程序崩溃。有人能帮忙吗Android fragments 点击发送按钮时应用程序崩溃?,android-fragments,android-asynctask,Android Fragments,Android Asynctask,我想将编辑文本中的数据发布到数据库/php脚本,并在列表视图中以编辑文本的相同片段自动显示该数据,但当单击“发送”按钮时,我的应用程序崩溃。有人能帮忙吗 import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONException; i
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.truevision.cypher.library.JSONParser;
public class FragmentOne extends ListFragment {
public static final String IMAGE_RESOURCE_ID = "iconResourceID";
public static final String ITEM_NAME = "itemName";
ListView listView;
EditText editText;
Button sendBtn;
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
// url to create new post
private static final String url_post_message = "http://example.com/myscript.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_layout_one,container, false);
sendBtn = (Button) view.findViewById(R.id.sendBtn);
// button click event
sendBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// creating new post in background thread
new Post().execute("");
}
});
return view;
}
/**
* Background Async Task to Create new post
* */
class Post extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Posting..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating post
* */
protected String doInBackground(String... args) {
String messages = editText.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("messages", messages));
// getting JSON Object
// Note that create post url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_post_message,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created post
} else {
// failed to create post
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
import java.util.ArrayList;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.ListFragment;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListView;
导入com.truevision.cypher.library.JSONParser;
公共类FragmentOne扩展了ListFragment{
公共静态最终字符串IMAGE\u RESOURCE\u ID=“iconResourceID”;
公共静态最终字符串ITEM_NAME=“itemName”;
列表视图列表视图;
编辑文本编辑文本;
按钮sendBtn;
//进度对话框
私人对话;
JSONParser JSONParser=新的JSONParser();
//创建新帖子的url
私有静态最终字符串url\u post\u消息=”http://example.com/myscript.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(右布局。碎片布局,容器,错误);
sendBtn=(按钮)view.findViewById(R.id.sendBtn);
//按钮点击事件
sendBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//在后台线程中创建新帖子
new Post().execute(“”);
}
});
返回视图;
}
/**
*创建新帖子的后台异步任务
* */
类Post扩展异步任务{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(getActivity());
pDialog.setMessage(“发布…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
/**
*创建帖子
* */
受保护的字符串doInBackground(字符串…args){
String messages=editText.getText().toString();
//建筑参数
List params=new ArrayList();
添加(新的BasicNameValuePair(“消息”,消息));
//获取JSON对象
//请注意,CreatePostURL接受post方法
JSONObject json=jsonParser.makeHttpRequest(url\u post\u消息,
“POST”,params);
//检查cat fro响应日志
d(“创建响应”,json.toString());
//检查成功标签
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
//已成功创建帖子
}否则{
//无法创建帖子
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//完成后关闭对话框
pDialog.disclose();
}
}
}
PHP代码
<?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['messages'])) {
$messages = $_POST['messages'];
// include db connect class
require_once 'DB_Connect.php';
// connecting to db
$db = new DB_Connect();
// mysql inserting a new row
$result = mysql_query("INSERT INTO post(messages) VALUES('$messages')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Post successfully added.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
问题是NullPointerException,因此某些内容未初始化。查看代码似乎
在doInBackground中使用editText,但未初始化。首先使用view.findviewbyd对其进行初始化。可以发布日志吗?有什么例外?谢谢你帮我解决我的新手错误。真不敢相信我错过了。它现在确实起作用了。再次感谢@代码1完美,投票回答:-)被接受,再见
07-26 17:37:53.489: D/OpenGLRenderer(28074): Flushing caches (mode 0)
07-26 17:38:01.046: I/Adreno200-EGLSUB(28589): <ConfigWindowMatch:2218>: Format RGBA_8888.
07-26 17:38:01.056: D/memalloc(28589): /dev/pmem: Mapped buffer base:0x5bd46000 size:13352960 offset:11816960 fd:55
07-26 17:38:01.056: D/OpenGLRenderer(28589): Enabling debug mode 0
07-26 17:38:01.256: D/memalloc(28589): /dev/pmem: Mapped buffer base:0x5cb02000 size:21319680 offset:19783680 fd:58
07-26 17:38:02.187: D/memalloc(28589): /dev/pmem: Mapped buffer base:0x5e057000 size:11816960 offset:10280960 fd:61
07-26 17:38:14.429: I/Adreno200-EGLSUB(28589): <ConfigWindowMatch:2218>: Format RGBA_8888.
07-26 17:38:14.429: D/memalloc(28589): /dev/pmem: Mapped buffer base:0x5ed9c000 size:16711680 offset:16404480 fd:64
07-26 17:38:14.459: D/memalloc(28589): /dev/pmem: Mapped buffer base:0x5fd8c000 size:13762560 offset:13455360 fd:70
07-26 17:38:14.499: D/memalloc(28589): /dev/pmem: Mapped buffer base:0x60aac000 size:14069760 offset:13762560 fd:73
07-26 17:38:14.650: W/dalvikvm(28589): threadid=11: thread exiting with uncaught exception (group=0x40cc31f8)
07-26 17:38:15.010: E/AndroidRuntime(28589): FATAL EXCEPTION: AsyncTask #1
07-26 17:38:15.010: E/AndroidRuntime(28589): java.lang.RuntimeException: An error occured while executing doInBackground()
07-26 17:38:15.010: E/AndroidRuntime(28589): at android.os.AsyncTask$3.done(AsyncTask.java:278)
07-26 17:38:15.010: E/AndroidRuntime(28589): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-26 17:38:15.010: E/AndroidRuntime(28589): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-26 17:38:15.010: E/AndroidRuntime(28589): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-26 17:38:15.010: E/AndroidRuntime(28589): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-26 17:38:15.010: E/AndroidRuntime(28589): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-26 17:38:15.010: E/AndroidRuntime(28589): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-26 17:38:15.010: E/AndroidRuntime(28589): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-26 17:38:15.010: E/AndroidRuntime(28589): at java.lang.Thread.run(Thread.java:856)
07-26 17:38:15.010: E/AndroidRuntime(28589): Caused by: java.lang.NullPointerException
07-26 17:38:15.010: E/AndroidRuntime(28589): at com.truevision.cypher.FragmentOne$Post.doInBackground(FragmentOne.java:85)
07-26 17:38:15.010: E/AndroidRuntime(28589): at com.truevision.cypher.FragmentOne$Post.doInBackground(FragmentOne.java:1)
07-26 17:38:15.010: E/AndroidRuntime(28589): at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-26 17:38:15.010: E/AndroidRuntime(28589): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-26 17:38:15.010: E/AndroidRuntime(28589): ... 5 more
07-26 17:38:15.310: D/OpenGLRenderer(28589): Flushing caches (mode 0)
07-26 17:38:15.310: D/memalloc(28589): /dev/pmem: Unmapping buffer base:0x5bd46000 size:13352960 offset:11816960
07-26 17:38:15.310: D/memalloc(28589): /dev/pmem: Unmapping buffer base:0x5cb02000 size:21319680 offset:19783680
07-26 17:38:15.310: D/memalloc(28589): /dev/pmem: Unmapping buffer base:0x5e057000 size:11816960 offset:10280960
07-26 17:38:15.330: D/OpenGLRenderer(28589): Flushing caches (mode 0)
07-26 17:38:15.340: D/memalloc(28589): /dev/pmem: Unmapping buffer base:0x5ed9c000 size:16711680 offset:16404480
07-26 17:38:15.340: D/memalloc(28589): /dev/pmem: Unmapping buffer base:0x5fd8c000 size:13762560 offset:13455360
07-26 17:38:15.340: D/memalloc(28589): /dev/pmem: Unmapping buffer base:0x60aac000 size:14069760 offset:13762560
07-26 17:38:15.841: D/OpenGLRenderer(28589): Flushing caches (mode 1)
07-26 17:38:16.591: E/WindowManager(28589): Activity com.truevision.cypher.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41689f50 that was originally added here
07-26 17:38:16.591: E/WindowManager(28589): android.view.WindowLeaked: Activity com.truevision.cypher.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41689f50 that was originally added here
07-26 17:38:16.591: E/WindowManager(28589): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
07-26 17:38:16.591: E/WindowManager(28589): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
07-26 17:38:16.591: E/WindowManager(28589): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
07-26 17:38:16.591: E/WindowManager(28589): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
07-26 17:38:16.591: E/WindowManager(28589): at android.view.Window$LocalWindowManager.addView(Window.java:537)
07-26 17:38:16.591: E/WindowManager(28589): at android.app.Dialog.show(Dialog.java:278)
07-26 17:38:16.591: E/WindowManager(28589): at com.truevision.cypher.FragmentOne$Post.onPreExecute(FragmentOne.java:78)
07-26 17:38:16.591: E/WindowManager(28589): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
07-26 17:38:16.591: E/WindowManager(28589): at android.os.AsyncTask.execute(AsyncTask.java:511)
07-26 17:38:16.591: E/WindowManager(28589): at com.truevision.cypher.FragmentOne$1.onClick(FragmentOne.java:56)
07-26 17:38:16.591: E/WindowManager(28589): at android.view.View.performClick(View.java:3511)
07-26 17:38:16.591: E/WindowManager(28589): at android.view.View$PerformClick.run(View.java:14111)
07-26 17:38:16.591: E/WindowManager(28589): at android.os.Handler.handleCallback(Handler.java:605)
07-26 17:38:16.591: E/WindowManager(28589): at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 17:38:16.591: E/WindowManager(28589): at android.os.Looper.loop(Looper.java:137)
07-26 17:38:16.591: E/WindowManager(28589): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-26 17:38:16.591: E/WindowManager(28589): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 17:38:16.591: E/WindowManager(28589): at java.lang.reflect.Method.invoke(Method.java:511)
07-26 17:38:16.591: E/WindowManager(28589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
07-26 17:38:16.591: E/WindowManager(28589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
07-26 17:38:16.591: E/WindowManager(28589): at dalvik.system.NativeStart.main(Native Method)