使按钮在片段中工作而不是活动

时间:2015-02-20 12:56:36

标签: android

我可以在我的活动中设置一个按钮,但是我在 Fragment 中使按钮eventlistener工作时遇到了问题。有人可以帮我找出错误的位置吗?我已经尝试了很长时间了。大约4个小时,但我无法弄清楚我做错了什么。

package com.fistats.fistats.util;


import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.fistats.fistats.MainActivity;
import com.fistats.fistats.R;
import com.fistats.fistats.remoteCom;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class CreateEx extends Fragment implements View.OnClickListener {
private static final String EXER_URL = "THE URL";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private EditText name, muscle, remedy, description;
private Button create;
private ProgressDialog pDialog;
private RelativeLayout theOne;
remoteCom rC = new remoteCom();


public CreateEx() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    theOne = (RelativeLayout) inflater.inflate(R.layout.fragment_createex, container, false);
    name = (EditText) theOne.findViewById(R.id.Ex);
    muscle = (EditText) theOne.findViewById(R.id.mu);
    description = (EditText) theOne.findViewById(R.id.des);
    remedy = (EditText) theOne.findViewById(R.id.rem);
    create = (Button)theOne.findViewById(R.id.saex);
    create.setOnClickListener(this);

    return theOne;

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub



        new CreateIt().execute();


}

class CreateIt extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    boolean failure = false;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Creating User...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;
        String nameIn = name.getText().toString();
        String muscleIn = muscle.getText().toString();
        String descriptionIn = description.getText().toString();
        String remedyIn = remedy.getText().toString();
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", nameIn));
            params.add(new BasicNameValuePair("muscle", muscleIn));
            params.add(new BasicNameValuePair("description", descriptionIn));
            params.add(new BasicNameValuePair("remedies", remedyIn));

            Log.d("request!", "starting");

            //Posting user data to script
            JSONObject json = rC.makeHttpRequest(EXER_URL, "POST", params);

            // full json response
            Log.d("Exercise reg attempt", json.toString());

            // json success element
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                Log.d("Exercise Created!", json.toString());
                return json.getString(TAG_MESSAGE);
            }else{
                Log.d("submitting failed!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);

            }
        } 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 product deleted
        pDialog.dismiss();
        if (file_url != null){
            Toast.makeText(getActivity(), file_url, Toast.LENGTH_LONG).show();
        }

    }

}

}

这是日志中发生的事情:

02-20 10:24:12.085    1930-1930/com.fistats.fistats I/art﹕ Not late-enabling -Xcheck:jni (already on)
02-20 10:24:12.214    1930-1949/com.fistats.fistats D/OpenGLRenderer﹕ Render dirty regions requested: true
02-20 10:24:12.256    1930-1930/com.fistats.fistats D/﹕ HostConnection::get() New Host Connection established 0xa6b04350, tid 1930
02-20 10:24:12.333    1930-1930/com.fistats.fistats D/Atlas﹕ Validating map...
02-20 10:24:12.431    1930-1949/com.fistats.fistats D/﹕ HostConnection::get() New Host Connection established 0xa6b04580, tid 1949
02-20 10:24:12.517    1930-1949/com.fistats.fistats I/OpenGLRenderer﹕ Initialized EGL, version 1.4
02-20 10:24:12.572    1930-1949/com.fistats.fistats D/OpenGLRenderer﹕ Enabling debug mode 0
02-20 10:24:12.594    1930-1949/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:24:12.594    1930-1949/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6b4d400, error=EGL_SUCCESS
02-20 10:24:13.307    1930-1949/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:24:13.307    1930-1949/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6b4d400, error=EGL_SUCCESS
02-20 10:24:15.467    1930-1949/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:24:15.467    1930-1949/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0f87a0, error=EGL_SUCCESS
02-20 10:24:16.348    1930-1949/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:24:16.348    1930-1949/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa52f89e0, error=EGL_SUCCESS
02-20 10:24:59.099    1930-2277/com.fistats.fistats D/request!﹕ starting
02-20 10:24:59.139    1930-1949/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:24:59.139    1930-1949/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa52f8ae0, error=EGL_SUCCESS
02-20 10:24:59.187    1930-2277/com.fistats.fistats D/Login attempt﹕ {"success":1,"message":"Login successful!"}
02-20 10:24:59.187    1930-2277/com.fistats.fistats D/Login Successful!﹕ {"success":1,"message":"Login successful!"}
02-20 10:24:59.387    1930-1949/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:24:59.387    1930-1949/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5575420, error=EGL_SUCCESS
02-20 10:25:01.331    1930-1949/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:25:01.331    1930-1949/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa55757a0, error=EGL_SUCCESS
02-20 10:25:01.341    1930-1930/com.fistats.fistats I/Choreographer﹕ Skipped 117 frames!  The application may be doing too much work on its main thread.
02-20 10:25:03.681    1930-1930/com.fistats.fistats I/Choreographer﹕ Skipped 141 frames!  The application may be doing too much work on its main thread.
02-20 10:30:54.986    1930-1930/com.fistats.fistats W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
02-20 10:31:50.831    2367-2383/com.fistats.fistats D/OpenGLRenderer﹕ Render dirty regions requested: true
02-20 10:31:50.833    2367-2367/com.fistats.fistats D/﹕ HostConnection::get() New Host Connection established 0xa601d380, tid 2367
02-20 10:31:50.913    2367-2367/com.fistats.fistats D/Atlas﹕ Validating map...
02-20 10:31:50.989    2367-2383/com.fistats.fistats D/﹕ HostConnection::get() New Host Connection established 0xa601d660, tid 2383
02-20 10:31:51.064    2367-2383/com.fistats.fistats I/OpenGLRenderer﹕ Initialized EGL, version 1.4
02-20 10:31:51.130    2367-2383/com.fistats.fistats D/OpenGLRenderer﹕ Enabling debug mode 0
02-20 10:31:51.139    2367-2383/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:31:51.139    2367-2383/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa602c600, error=EGL_SUCCESS
02-20 10:31:53.962    2367-2383/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:31:53.962    2367-2383/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa602c620, error=EGL_SUCCESS
02-20 10:31:55.262    2367-2386/com.fistats.fistats D/request!﹕ starting
02-20 10:31:55.287    2367-2383/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:31:55.288    2367-2383/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5376120, error=EGL_SUCCESS
02-20 10:31:55.339    2367-2386/com.fistats.fistats D/Login attempt﹕ {"success":1,"message":"Login successful!"}
02-20 10:31:55.339    2367-2386/com.fistats.fistats D/Login Successful!﹕ {"success":1,"message":"Login successful!"}
02-20 10:31:55.806    2367-2379/com.fistats.fistats I/art﹕ Background sticky concurrent mark sweep GC freed 7148(404KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 2MB/2MB, paused 11.046ms total 25.204ms
02-20 10:31:56.547    2367-2367/com.fistats.fistats I/Choreographer﹕ Skipped 41 frames!  The application may be doing too much work on its main thread.
02-20 10:31:56.582    2367-2383/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:31:56.582    2367-2383/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa53a1200, error=EGL_SUCCESS
02-20 10:31:56.661    2367-2383/com.fistats.fistats W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-20 10:31:56.661    2367-2383/com.fistats.fistats W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa602c620, error=EGL_SUCCESS

2 个答案:

答案 0 :(得分:0)

您确定要在任务完成之前完成活动吗?您正在使用活动作为上下文初始化进度对话框:

pDialog = new ProgressDialog(getActivity());

在你打完活动结束后就会发生这种情况。我认为你应该完成你的任务,当它结束时,在onPostExecute上调用activity.finish。如果要关闭活动并使任务保持活动状态,则必须将应用程序上下文传递给ProgressDialog

答案 1 :(得分:0)

这就是你需要的onCreateView方法

return (inflater.inflate(R.layout.fragment_createex,container,false));

然后您需要覆盖onActivityCreated并声明所有小部件,如:

public void onActivityCreated(Bundle bundle){
    super.onActivityCreated(bundle)
    name = (EditText) getActivity().findViewById(R.id.Ex);
    muscle = (EditText) getActivity().findViewById(R.id.mu);
    description = (EditText) getActivity().findViewById(R.id.des);
    remedy = (EditText) getActivity().findViewById(R.id.rem);
    create = (Button)getActivity().findViewById(R.id.saex);
    create.setOnClickListener(this);
}