java.lang.ClassCastException:com.example.projjava.MainActivity无法转换为com.example.projjava.DbConnection

问题描述 投票:-1回答:1

每当我尝试运行程序时,都会收到此错误。该程序运行,但是当我单击“获取名称”时,它使我回到登录屏幕。

java.lang.ClassCastException:com.example.projjava.MainActivity无法转换为com.example.projjava.DbConnection

这是我的DbConnection.java文件:


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_profile);

        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.INTERNET}, PackageManager.PERMISSION_GRANTED);
        textView = findViewById(R.id.textView);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {
            Class.forName(Classes);
            connection = DriverManager.getConnection(url, username,password);
            textView.setText("SUCCESS");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            textView.setText("ERROR");
        } catch (SQLException e) {
            e.printStackTrace();
            textView.setText("FAILURE");
        }
    }

    public void getName(View view){
        if (connection!=null){
            Statement statement = null;
            try {
                statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery("Select fullName from userData;");
                while (resultSet.next()){
                    textView.setText(resultSet.getString(1));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        else {
            textView.setText("Connection is null");
        }
    }
}

这是我的FragmentProfile.java文件:

public class FragmentProfile extends Fragment implements View.OnClickListener {
    View view;
    Button LogOut;
    Button getName;

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

        LogOut = (Button)  view.findViewById(R.id.btnLogOut);
        LogOut.setOnClickListener(this);

        getName = (Button) view.findViewById(R.id.getName);
        getName.setOnClickListener(this);

        return view;

    }

    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btnLogOut:
                getActivity().finish(); // do your code
                break;

            case R.id.getName:
                ((DbConnection)getActivity()).getName(view); // do your code
                break;

            default:
                break;
        }


    }


}
java android sqlite android-fragments
1个回答
0
投票

[当我们谈论转换时:如果class A不扩展class B,则无法将class A转换为class B。这就是得到java.lang.ClassCastException的原因。

您的com.example.projjava.MainActivity不是extends中的com.example.projjava.DbConnection。因此,您不能编写这样的语句:

    case R.id.getName:
        // The error is here
        ((DbConnection)getActivity()).getName(view); // do your code
    break;

应该是

    case R.id.getName:
        ((MainActivity) getActivity()).getName(view); // do your code
    break;

为了稍微改善您的情况,请创建一个singleton class,如果有可用的话,您可以从中获得DbConnection。使用此模式,您还可以管理一次可以打开的DbConnection的数量。通常,数据库连接很昂贵,您应尽可能减少它们的数量。

[还有其他一些技术,例如dependency injection,尤其是对于Android模式(例如MVVM),您应该及时熟悉。我保证他们会为您提供很多帮助!

投入您的时间在学习上。这是您在时间上可以做的最好的事情!

© www.soinside.com 2019 - 2024. All rights reserved.