在片段中使用asynctask我的app强制关闭

时间:2014-11-23 07:13:42

标签: android

我更改扩展活动以扩展片段,我的AsynkTask类在扩展活动中运行良好,但在扩展片段中使用时,登录到我的应用程序时关闭我调用类DownloadJson在点击我应该怎么做请帮助我

public class OmegaFragment extends Fragment implements View.OnClickListener {



JSONObject jsonobject;
JSONArray jsonarray;
private PendingIntent pendingIntent; 
EditText ocUserName ;
 EditText ocPassword; 

View view;




@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_omega, container, false);

}




   private class DownloadJSON extends AsyncTask<Void, Void, Integer> {



    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Integer doInBackground(Void... params) {
        // Create an array
        int isCorrect= 0;
        jsonarray = JSONfunctions.getJSONfromURL(mUrl);


                    EditText ocUserName1 =(EditText)view.findViewById(R.id.userNameText);
                    EditText ocPassword1 =(EditText)view.findViewById(R.id.passwordText);
                    isCorrect = 1;



        return isCorrect;
    }

    @Override
    protected void onPostExecute(Integer  args) {
        mFragmentCallback.onTaskDone();
        super.onPostExecute(args);
        mProgressDialog.dismiss();
    }
}
 public void onClick(View v) {    
    if ( v.getId() == R.id.button1 )
    {
        Button button = (Button) v;

      //  EditText ocUserName =(EditText)view.findViewById(R.id.userNameText);
     //   EditText ocPassword =(EditText)view.findViewById(R.id.passwordText);
        boolean flag=true;






                            mUrl = global.apiurl +":"+global.apiport+ "/"+global.apiname+"/"+global.controller+"/" + ocUserName.getText().toString() + "/" + ocPassword.getText().toString() + "/0";


                        try {
                             isAccess = (new DownloadJSON().execute()).get();
                        } catch (InterruptedException e) {
                            e.printStackTrace();  
                        } catch (ExecutionException e) {
                            e.printStackTrace();  
                        }
                        if ( isAccess == 1 )
                        {
                            Intent intentMain = new Intent(getActivity() ,hozeactivity.class);

logcat输出:

    11-22 23:19:18.822: ERROR/AndroidRuntime(1292): FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.mesbahSoft.quran.OmegaFragment.onClick(OmegaFragment.java:248)
    at android.view.View.performClick(View.java:3460)
    at android.view.View$PerformClick.run(View.java:13955)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4340)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

正如我在找到你的观点之前告诉你的那样。简而言之,在doInBackground方法中删除这行代码并在onCreateView中移动它们。像这样:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_omega, container, false);
    EditText ocUserName1 =(EditText)view.findViewById(R.id.userNameText);
    EditText ocPassword1 =(EditText)view.findViewById(R.id.passwordText);

}

通过这种方式,您将修复空指针异常,但如果要将请求发送到服务器,则必须调用AsynchTask。 其余的我建议你看看:

档案 - &gt;新 - &gt;其他 - &gt; Android活动 - &gt; LoginActivity

它有一个很好的样本供你开始。

答案 1 :(得分:0)

更改您的OncreateView()方法,如下所示

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_omega, container, false);
    ocUserName1 =(EditText)view.findViewById(R.id.userNameText);
    ocPassword1 =(EditText)view.findViewById(R.id.passwordText);

}

实际上你在OnClick方法中使用ocUserName1,ocPassword1但是你没有初始化它们。在你创建时,你只为本地变量赋值,这就是为什么会出现问题。