首页 > 解决方案 > java.lang.InstantiationException: java.lang.Class 不能在 java.lang.Class.newInstance(Native Method) 实例化

问题描述

我正在尝试在我的小项目中添加一个带有评分栏和更多小部件的自定义对话框,但遇到了这个错误。有点初学者,所以如果你能对我来说简单一点,我会很高兴,谢谢

我试图将类更改为抽象 java 类。

  package com.a.shon.scoutszone2;

    import android.app.Activity;
    import android.app.Dialog;
    import android.content.Intent;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.RatingBar;
    import android.widget.TextView;
    import android.widget.Toast;

    public  class ZoneActivity extends Activity implements View.OnClickListener  {

    Button btnPeula;
    ImageView ivMap;
    ImageView ivZone; //Will Change in the future
    //ListView lstZone;
    Zone current = new Zone();
    private Drawable[] maps;
    @Override
    protected void onCreate(Bundle savedInstanceState) { //add a dialog/menu to logout and manager activities
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zone);

        btnPeula = findViewById(R.id.btnPeula);
        ivMap = findViewById(R.id.ivMap);
        maps = new Drawable[4];
        maps[0] = getResources().getDrawable(R.drawable.map, null);
        ivMap.setImageDrawable(maps[0]);
        ivZone=findViewById(R.id.ivZone);//Will Change in the future
        maps[1] = getResources().getDrawable(R.drawable.tzone, null);//Will Change in the future
        ivZone.setImageDrawable(maps[1]); //Will Change in the future

        //lstZone = findViewById(R.id.lstZone);
        //lstZone.setOnItemClickListener(this);
        // lstZone.setAdapter( new ArrayAdapter<Zone>(this, android.R.layout))
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.the_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item)
    {
        super.onOptionsItemSelected(item);
        switch (item.getItemId())
        {
            case R.id.itemManager:
                Toast.makeText(this, "Manager...", Toast.LENGTH_SHORT).show();
                Intent manager = new Intent(this, manager.class);
                startActivity(manager);
                break;
            case R.id.itemLogout:
                Toast.makeText(this, "Logging out...", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case R.id.itemRating: //make it a dialog not an activity!
                Toast.makeText(this, "Start Rating!", Toast.LENGTH_SHORT).show();
                showCustomDialog();
                break;
        }
        return true;
    }

    public void onPeula(View v)
    {
        Intent peula = new Intent(this, PeulotActivity.class);
        startActivity(peula);
    }

    private void showCustomDialog()
    {
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.custom_dialog);
        // set the custom dialog components - text, image and button
        final RatingBar mRatingbar = (RatingBar) findViewById(R.id.mRatingbar);
        final TextView tvRatingscale = (TextView) findViewById(R.id.tvRatingScale);
        final EditText etFeedback = (EditText) findViewById(R.id.etFeedback);
        Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
        mRatingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                tvRatingscale.setText(String.valueOf(v));
                switch ((int) ratingBar.getRating()) {
                    case 1:
                        tvRatingscale.setText("Very bad");
                        break;
                    case 2:
                        tvRatingscale.setText("Need some improvement");
                        break;
                    case 3:
                        tvRatingscale.setText("Good");
                        break;
                    case 4:
                        tvRatingscale.setText("Great");
                        break;
                    case 5:
                        tvRatingscale.setText("Awesome. I love it");
                        break;
                    default:
                        tvRatingscale.setText("");
                }
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // if (etFeedback.getText().toString().isEmpty()) {
                //     Toast.makeText(ZoneActivity.this, "Please fill in feedback text box", Toast.LENGTH_LONG).show();
                // } else {
                etFeedback.setText("");
                mRatingbar.setRating(0);
                //Toast.makeText(ZoneActivity.this, "Thank you for sharing your feedback", Toast.LENGTH_SHORT).show();
            }
            // }
        });

        dialog.show();
    }


    @Override
    public void onClick(View v)
    {
        Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
    }
}

我希望当打开对话框时应用程序不会崩溃,但实际上每次单击打开对话框的按钮时,应用程序都会崩溃
java.lang.InstantiationException: java.lang.Class cannot be instantiated at java.lang .Class.newInstance(本机方法)

标签: android

解决方案


您需要使用 Builder 来构建您的对话框,而不是直接实例化它。

对话框文档

Dialog 类是对话框的基类,但应避免直接实例化 Dialog。相反,请使用以下子类之一:

AlertDialog 可以显示标题、最多三个按钮、可选项目列表或自定义布局的对话框。

代码示例:

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = requireActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
    // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // sign in the user ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });

此外,如果您想正确处理生命周期事件,请考虑使用 aDialogFragment并在该片段中实例化您的对话框。


推荐阅读