Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:53:45+00:00 2026-06-13T16:53:45+00:00

The android code is simple: public void createNewTask(View v) { try { MyTask task

  • 0

The android code is simple:

public void createNewTask(View v) {
    try {
        MyTask task = MyTask.class.newInstance();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

private class MyTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        return null;  
    }
}

But when invoking createNewTask method, it will throw an exception:

Caused by: java.lang.RuntimeException: 
    java.lang.InstantiationException: com.example.MyActivity$MyTask
    at com.example.MyActivity.createNewTask(MyActivity.java:214)
    ... 14 more
    Caused by: java.lang.InstantiationException: com.example.MyActivity$MyTask
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1440)
    at com.example.MyActivity.createNewTask(MyActivity.java:211)

But if I choose another class, e.g.

MyActivity task = MyActivity.class.newInstance();

Everything will be fine.

Where is wrong in my code? I tested it in an android phone with android 2.3.6.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:53:46+00:00Added an answer on June 13, 2026 at 4:53 pm

    Found two ways to solve this problem:

    public static class MyTask {
    }
    

    or

    private static class MyTask {
        public MyTask(){}
    }
    

    According to documentation:

    InstantiationException – if this Class represents an abstract class,
    an interface, an array class, a primitive type, or void; or if the
    class has no nullary constructor; or if the instantiation fails for
    some other reason.

    Try adding nullary constructor to your class:

    public MyTask() {}
    

    EDIT

    According to this documentation:

    Class.newInstance() requires that the constructor be visible;
    Constructor.newInstance() may invoke private constructors under
    certain circumstances.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple java code: package com.androiddesktoptest.testtest; public class AndroidDesktopTestMain { public static
I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
Beginner in Android development. My code crashes. I have made a simple Java method
From my android code I try with the android browser to access a tomcat
Very simple code for test: interface Base { void interfaceTest(); static final String m
I am using this simple code from here: https://developers.facebook.com/docs/mobile/android/build/#sig public class MainActivity extends Activity
I'm new to android development and I'm trying to code a simple Simon-like game
I have a very simple code, as I am new to Java and Android.
I have code for simple audio player on android. But it is not working
I am trying to code a simple chat app for Android so I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.