android – 类型类型不匹配:无法从RegisterFragment转换为Fragment

android – 类型类型不匹配:无法从RegisterFragment转换为Fragment,第1张

概述这是我的代码,问题发生在Switch Case …. plz帮助 package com.example.atg.adapter; import com.example.atg.LoginFragment;import com.example.atg.RegisterFragment;import android.support.v4.app.Fragment;import android. 这是我的代码,问题发生在Switch Case …. plz帮助

package com.example.atg.adapter;

import com.example.atg.LoginFragment;import com.example.atg.RegisterFragment;import androID.support.v4.app.Fragment;import androID.support.v4.app.FragmentManager;import androID.support.v4.app.FragmentStatePagerAdapter;public class TabsPagerAdapter extends FragmentStatePagerAdapter {    public TabsPagerAdapter(FragmentManager fm) {        super(fm);        // Todo auto-generated constructor stub    }    @OverrIDe    public Fragment getItem(int i) {        switch (i) {        case 0:            //Fragement for AndroID Tab            return new LoginFragment();        case 1:           //Fragment for Ios Tab            return new RegisterFragment();        }        return null;    }    @OverrIDe    public int getCount() {        // Todo auto-generated method stub        return 2; //No of Tabs    }    }

RegisterFragment类

package com.example.atg;    import androID.app.Activity;    import androID.app.Fragment;    import androID.app.ProgressDialog;    import androID.content.Context;    import androID.content.Intent;    import androID.net.ConnectivityManager;    import androID.net.NetworkInfo;    import androID.os.AsyncTask;    import androID.os.Bundle;    import androID.vIEw.VIEw;    import androID.Widget.button;    import androID.Widget.EditText;    import androID.Widget.TextVIEw;    import androID.Widget.Toast;    import com.example.atg.library.DatabaseHandler;    import com.example.atg.library.UserFunctions;    import org.Json.JsONException;    import org.Json.JsONObject;    import java.io.IOException;    import java.net.httpURLConnection;    import java.net.MalformedURLException;    import java.net.URL;

public class RegisterFragment extends Fragment {

/**     *  JsON Response node names.     **/    private static String KEY_SUCCESS = "success";    private static String KEY_UID = "uID";    private static String KEY_FirsTname = "fname";    private static String KEY_LASTname = "lname";    private static String KEY_USERname = "uname";    private static String KEY_EMAIL = "email";    private static String KEY_CREATED_AT = "created_at";    private static String KEY_ERROR = "error";    /**     * Defining layout items.     **/    EditText inputFirstname;    EditText inputLastname;    EditText inputUsername;    EditText inputEmail;    EditText inputPassword;    button btnRegister;    TextVIEw registerErrorMsg;    /**     * Called when the activity is first created.     */    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);       setContentVIEw(R.layout.register);    /**     * Defining all layout items     **/        inputFirstname = (EditText) findVIEwByID1(R.ID.fname);        inputLastname = (EditText) findVIEwByID1(R.ID.lname);        inputUsername = (EditText) findVIEwByID1(R.ID.uname);        inputEmail = (EditText) findVIEwByID1(R.ID.email);        inputPassword = (EditText) findVIEwByID1(R.ID.pword);        btnRegister = (button) findVIEwByID(R.ID.register);        registerErrorMsg = (TextVIEw) findVIEwByID(R.ID.register_error);/** * button which Switches back to the login screen on clicked **/        button login = (button) findVIEwByID(R.ID.bktologin);        login.setonClickListener(new VIEw.OnClickListener() {            public voID onClick(VIEw vIEw) {                Intent myIntent = new Intent(vIEw.getContext(),Login.class);                startActivityForResult(myIntent,0);            }        });        /**         * Register button click event.         * A Toast is set to alert when the fIElds are empty.         * Another toast is set to alert Username must be 5 characters.         **/        btnRegister.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                if (  ( !inputUsername.getText().toString().equals("")) && ( !inputPassword.getText().toString().equals("")) && ( !inputFirstname.getText().toString().equals("")) && ( !inputLastname.getText().toString().equals("")) && ( !inputEmail.getText().toString().equals("")) )                {                    if ( inputUsername.getText().toString().length() > 4 ){                    NetAsync(vIEw);                    }                    else                    {                        Toast.makeText(getApplicationContext(),"Username should be minimum 5 characters",Toast.LENGTH_SHORT).show();                    }                }                else                {                    Toast.makeText(getApplicationContext(),"One or more fIElds are empty",Toast.LENGTH_SHORT).show();                }            }        });       }    protected Context getApplicationContext() {        // Todo auto-generated method stub        return null;    }    private EditText findVIEwByID1(int fname) {        // Todo auto-generated method stub        return null;    }    private button findVIEwByID(int register){        return null;    }    private voID setContentVIEw(int register) {        // Todo auto-generated method stub    }    /**     * Async Task to check whether internet connection is working     **/    private class NetCheck extends AsyncTask<String,String,Boolean>    {        private ProgressDialog nDialog;        @OverrIDe        protected voID onPreExecute(){            super.onPreExecute();            nDialog.setMessage("Loading..");            nDialog.setTitle("Checking Network");            nDialog.setIndeterminate(false);            nDialog.setCancelable(true);            nDialog.show();        }        @OverrIDe        protected Boolean doInBackground(String... args){/** * Gets current device state and checks for working internet connection by trying Google. **/            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);            NetworkInfo netInfo = cm.getActiveNetworkInfo();            if (netInfo != null && netInfo.isConnected()) {                try {                    URL url = new URL("http://www.Google.com");                    httpURLConnection urlc = (httpURLConnection) url.openConnection();                    urlc.setConnectTimeout(3000);                    urlc.connect();                    if (urlc.getResponseCode() == 200) {                        return true;                    }                } catch (MalformedURLException e1) {                    // Todo auto-generated catch block                    e1.printstacktrace();                } catch (IOException e) {                    // Todo auto-generated catch block                    e.printstacktrace();                }            }            return false;        }        @OverrIDe        protected voID onPostExecute(Boolean th){            if(th == true){                nDialog.dismiss();                new ProcessRegister().execute();            }            else{                nDialog.dismiss();                registerErrorMsg.setText("Error in Network Connection");            }        }    }    private class ProcessRegister extends AsyncTask<String,JsONObject> {/** * Defining Process dialog **/        private ProgressDialog pDialog;        String email,password,fname,lname,uname;        @OverrIDe        protected voID onPreExecute() {            super.onPreExecute();            inputUsername = (EditText) findVIEwByID1(R.ID.uname);            inputPassword = (EditText) findVIEwByID1(R.ID.pword);               fname = inputFirstname.getText().toString();               lname = inputLastname.getText().toString();                email = inputEmail.getText().toString();                uname= inputUsername.getText().toString();                password = inputPassword.getText().toString();            pDialog.setTitle("Contacting Servers");            pDialog.setMessage("Registering ...");            pDialog.setIndeterminate(false);            pDialog.setCancelable(true);            pDialog.show();        }        @OverrIDe        protected JsONObject doInBackground(String... args) {        UserFunctions userFunction = new UserFunctions();        JsONObject Json = userFunction.registerUser(fname,email,uname,password);            return Json;        }       @OverrIDe        protected voID onPostExecute(JsONObject Json) {       /**        * Checks for success message.        **/                try {                    if (Json.getString(KEY_SUCCESS) != null) {                        registerErrorMsg.setText("");                        String res = Json.getString(KEY_SUCCESS);                        String red = Json.getString(KEY_ERROR);                        if(Integer.parseInt(res) == 1){                            pDialog.setTitle("Getting Data");                            pDialog.setMessage("Loading Info");                            registerErrorMsg.setText("Successfully Registered");                            DatabaseHandler db = new DatabaseHandler(getApplicationContext());                            JsONObject Json_user = Json.getJsONObject("user");                            /**                             * Removes all the prevIoUs data in the sqlite database                             **/                            UserFunctions logout = new UserFunctions();                            logout.logoutUser(getApplicationContext());                            db.addUser(Json_user.getString(KEY_FirsTname),Json_user.getString(KEY_LASTname),Json_user.getString(KEY_EMAIL),Json_user.getString(KEY_USERname),Json_user.getString(KEY_UID),Json_user.getString(KEY_CREATED_AT));                            /**                             * Stores registered data in sqlite Database                             * Launch Registered screen                             **/                            Intent registered = new Intent(getApplicationContext(),Registered.class);                            /**                             * Close all vIEws before launching Registered screen                            **/                            registered.addFlags(Intent.FLAG_ACTIVITY_CLEAR_top);                            pDialog.dismiss();                            startActivity(registered);                              finish();                        }                        else if (Integer.parseInt(red) ==2){                            pDialog.dismiss();                            registerErrorMsg.setText("User already exists");                        }                        else if (Integer.parseInt(red) ==3){                            pDialog.dismiss();                            registerErrorMsg.setText("InvalID Email ID");                        }                    }                        else{                        pDialog.dismiss();                            registerErrorMsg.setText("Error occured in registration");                        }                } catch (JsONException e) {                    e.printstacktrace();                }            }}        public voID NetAsync(VIEw vIEw){            new NetCheck().execute();        }        public voID finish() {            // Todo auto-generated method stub        }        public ConnectivityManager getSystemService(String connectivityService) {            // Todo auto-generated method stub            return null;        }}

错误:

说明资源路径位置类型

Type mismatch: cannot convert from LoginFragment to Fragment    TabsPagerAdapter.java   /ATG/src/com/example/atg/adapter    line 20 Java Problem            Type mismatch: cannot convert from RegisterFragment to Fragment TabsPagerAdapter.java   /ATG/src/com/example/atg/adapter    line 23 Java Problem
解决方法 问题是你的RegisterFragment是一个androID.app.Fragment,但你在你的getItem(…)方法中返回一个androID.support.v4.app.Fragment.检查您导入Fragment的两个类中的import语句,并确保它们根据应用程序的需要导入正常的androID.app.Fragment或支持库androID.support.v4.app.Fragment. 总结

以上是内存溢出为你收集整理的android – 类型类型不匹配:无法从RegisterFragment转换为Fragment全部内容,希望文章能够帮你解决android – 类型类型不匹配:无法从RegisterFragment转换为Fragment所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1133261.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存