类型类型不匹配:不能从RegisterFragment转换为片段。

时间:2023-01-12 13:53:32

Here is my Code and The problem occurs in Switch Case....plz help

这是我的代码,这个问题发生在切换实例....请帮助

package com.example.atg.adapter;

包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 class

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 {

公共类RegisterFragment扩展片段{

    /**
     *  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, 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, lname, 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;
        }

}

Errors:

错误:

Description Resource Path Location Type

描述资源路径位置类型。

            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

2 个解决方案

#1


49  

The problem is that your RegisterFragment is a android.app.Fragment, but you are returning a android.support.v4.app.Fragment in your getItem(...) method. Check your import statements in both classes where you are importing Fragment and make sure they are either both importing the normal android.app.Fragment or the support library android.support.v4.app.Fragment depending on your application's needs.

问题是,您的RegisterFragment是一个android.app。片段,但您正在返回一个android.support.v4.app。您的getItem(…)方法中的片段。在导入片段的两个类中检查导入语句,并确保它们都导入了正常的android.app。片段或支持库android.support.v4.app。根据应用程序的需要分段。

#2


8  

the problem is you are importing

问题是您正在导入。

import android.app.Fragment;

inside RegisterFragment and

在RegisterFragment和

Importing

进口

import android.support.v4.app.Fragment;

inside adapter.

在适配器。

If you are using Support v4 import import android.support.v4.app.Fragment; in RegisterFragment as well.

如果您正在使用支持v4导入导入android.support.v4.app.Fragment;在RegisterFragment。

#1


49  

The problem is that your RegisterFragment is a android.app.Fragment, but you are returning a android.support.v4.app.Fragment in your getItem(...) method. Check your import statements in both classes where you are importing Fragment and make sure they are either both importing the normal android.app.Fragment or the support library android.support.v4.app.Fragment depending on your application's needs.

问题是,您的RegisterFragment是一个android.app。片段,但您正在返回一个android.support.v4.app。您的getItem(…)方法中的片段。在导入片段的两个类中检查导入语句,并确保它们都导入了正常的android.app。片段或支持库android.support.v4.app。根据应用程序的需要分段。

#2


8  

the problem is you are importing

问题是您正在导入。

import android.app.Fragment;

inside RegisterFragment and

在RegisterFragment和

Importing

进口

import android.support.v4.app.Fragment;

inside adapter.

在适配器。

If you are using Support v4 import import android.support.v4.app.Fragment; in RegisterFragment as well.

如果您正在使用支持v4导入导入android.support.v4.app.Fragment;在RegisterFragment。