标签: instantiationexception

Java - 在类<?的泛型类上使用newInstance()扩展myClass>:InstantiationException

我正在尝试使用Generic Classes,我遇到了一个无法克服的障碍.简而言之,我遇到了一个错误,我不明白它为什么被抛出:InstantiationException

在文档中,它将此异常定义为:

当应用程序尝试使用类Class中的newInstance方法创建类的实例时抛出,但指定的类对象无法实例化,因为它是接口或是抽象类.

现在让我头疼的问题是我不使用abstract或interface关键字.我也听说过它可能是由于没有默认构造函数(我有).只是为了确定,我将代码减少到尽可能小,但仍然会出错:

package Sandbox;

public class Sandbox {


    public static void main(String[] args) {
        Sandbox box = new Sandbox();
    }

    public Sandbox() {
        aMethod(subThread.class);
    }

    public void aMethod(Class<? extends superThread> type) {
        try {

            System.out.println("isInterface: "+type.isInterface());
            System.out.println("isAssignableFrom of subThread: "+type.isAssignableFrom(subThread.class));

            superThread t = type.newInstance();

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    private class superThread { // implements Runnable {

        public superThread() {}

        public void run() {}
    }

    private class subThread …
Run Code Online (Sandbox Code Playgroud)

java generics subclass instantiationexception

2
推荐指数
1
解决办法
1918
查看次数

如何修复InstantiationException,无法实例化Fragment?

这是我的第一篇文章,所以我提前道歉...

我正在尝试编译一个简单的Drawer示例/练习应用程序.但是,当我点击列表视图中的项目时,它会崩溃.Logcat说它无法从android.app.Fragment实例化.我也试过使用android.support.v4库 - 假设我正确实现了它,这是可疑的 - 具有相同的结果.坦率地说,我不确定区别是什么.我完全难过了.请帮忙!

主要活动:

package pac.gazette;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    private String[] fragmentNames;
    private String[] fragmentAddresses;

    private DrawerLayout drawer;
    private ListView drawerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentNames = getResources().getStringArray(R.array.fragment_names);
        fragmentAddresses = getResources().getStringArray(
                R.array.fragment_options);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerList = (ListView) findViewById(R.id.left_drawer);
        drawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list, fragmentNames));
        drawerList.setOnItemClickListener (new CustomClickListener());

    } …
Run Code Online (Sandbox Code Playgroud)

android instantiationexception android-fragments fragmenttransaction navigation-drawer

2
推荐指数
1
解决办法
1万
查看次数

使用 XMLEncoder 时出现 java.lang.Instantiation 异常

我试图在我的 Java 程序中使用 XMLEncoder,但我收到了 java.lang.InstantiationException。以下是我正在使用的代码:

   /*
       Method for serialization.
   */
   public void serializeToXml(Object obj) throws FileNotFoundException{

        FileOutputStream fos = new FileOutputStream("/home/neeraj/xmlOP.xml"); 
        XMLEncoder encoder  =  new XMLEncoder(fos);
        encoder.writeObject(obj);
        encoder.close();
    }


   public static void main(String [] args){


        String uuid = UUID.randomUUID().toString();

        SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
        Date date = new Date();
        String tDate = format.format(date);

        ClassA a = new  ClassA(uuid,"expense","Pune",tDate,1,200,0,4);
        a.createAssociatedEvents(2);

        serializationExample serializer = new serializationExample();
        try {

            serializer.serializeToXml(a);

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

}
Run Code Online (Sandbox Code Playgroud)

除此之外:我还有两个类:classA 和 classB。这两个类都实现了 Serializable。ClassA 有一个 …

java exception xmlencoder instantiationexception

2
推荐指数
1
解决办法
6795
查看次数

java.lang.InstantiationException:无法实例化类?

我正在尝试使用自动表单生成一个Android应用程序 http://labs.makemachine.net/2010/04/android-form-generator/

我一直在阅读很多关于"InstantiationException"的话题,但没有解决我的问题.

这是我的活动

    package com.example.makemachine;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.LinearLayout.LayoutParams;

/**
 * FormActivity allows you to create dynamic form layouts based upon a json schema file. 
 * This class should be sub-classed. 
 * 
 * @author Jeremy Brown
 */
public abstract class FormActivity extends Activity
{
    public static String SCHEMA_KEY_TYPE        = …
Run Code Online (Sandbox Code Playgroud)

android instantiationexception android-activity

0
推荐指数
1
解决办法
8812
查看次数

Kotlin 实例化异常

这是我的第一个 Kotlin 项目之一,我正在关注 android 的 Codelabs 以快速了解 Kotlin。Android 开发对我来说并不陌生,但我在谷歌上搜索问题的解决方案我在 Kotlin 中找到了比 Java 多的教程和解决方案。是时候开始转向该语言了,因为 Android 已经转向“Kotlin 优先”。

我不熟悉 var/val 以及如何设置 onClick() 侦听器。在玩这个项目时,我尝试了很多东西,并决定重新使用 XML 中的 android:onClick,这样我就不必在 Activity 中创建全局变量。

它开始在我身上崩溃 java.lang.InstantiationException

我注释掉了 Activity 中的所有代码,并删除了 xml 中的 android:onClick。我仍然遇到同样的错误。重建和清理项目后,我试过Invalidate Cache / Restart也无济于事。

我确定我可以创建一个新项目并重新实现我离开的代码,但我不能在更大的项目中做到这一点。我不知道是什么导致了这个错误,以及为什么无论我尝试什么它都不会消失。

活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/roll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="2"
            android:textSize="36sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <Button
            android:id="@+id/btnRoll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:text="Roll"

            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/roll" />

        <Button
            android:id="@+id/btnCountUp"
            android:layout_width="wrap_content" …
Run Code Online (Sandbox Code Playgroud)

android instantiationexception kotlin

0
推荐指数
1
解决办法
548
查看次数