1. Download our Official Android App: Forums for Android!

Apps ClassName.class.newinstance() crashes

Discussion in 'Android Development' started by Leolicos, Jul 22, 2010.

  1. Leolicos

    Leolicos Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    54
    Joined:
    Jun 16, 2010

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    Ok, I've been working on my first program for some time now, and for a while I was able to startActivity with a new Intent that created a new class just fine and went to the page, but now, for some reason it crashes. At first I thought it was in the startActivity, but I noticed in the debugger that it crashes on ClassName.class.newinstance(). Even before I startActivity, I put in ClassName c = ClassName.class.newinstance(), and it crashes there. I have no idea why, and I didn't even change anything in the class, nor do I have a constructor for the class, so it's not in there (at least from what I think). Why would class.newinstance() throw an exception???
     

    Advertisement

  2. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    I don't understand what you are trying to accomplish with ClassName.class.newinstance(). I can't think of a scenario where you would ever need to use that.

    What are you trying to do?
     
  3. Leolicos

    Leolicos Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    54
    Joined:
    Jun 16, 2010

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    I'm just using that as test code. If you notice at the top of my post, the program crashes at startActivity(), then when I went in debug mode, I noticed in the debugger that it was crashing when it was trying to create a new instance of my ClassName, in the ClassName.class.newinstance() function. So, just to test it out, I put that in my code instead of startActivity() and it's still crashing, so the problem isn't startActivity(), it's when it tries to make a new instance of ClassName. Why would that be??
     
  4. markb

    markb Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    148
    Joined:
    Sep 14, 2009

    Sep 14, 2009
    148
    26
    63
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
    What class are you trying to instantiate. You refer to "ClassName" but I'm guessing that's not the actual name of the class.

    Could you show the actual code please?

    What kind of thing is the class you're trying to instantiate? Is it another activity, a View, or what?

    Can you create an instance using the conventional method, just to confirm it works?
    i.e.
    Code (Text):
    1.  
    2.   ClassName c = new ClassName() ;
    3.  
    If that fails (or doesn't compile) then you have a valuable clue.

    You mention that you get an exception. What is the exception?

    The more information you can provide the better.

    Mark
     
  5. Leolicos

    Leolicos Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    54
    Joined:
    Jun 16, 2010

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    ClassName extends Activity.

    OnClickListener m_clickListener = new OnClickListener()
    {
    public void onClick(View v)
    {
    Intent i;
    i = new Intent(cApp.this, cAboutPage.class);
    startActivity(i);
    //cAboutPage s = new cAboutPage();
    }
    };

    public class cAboutPage extends Activity
    {
    ImageView m_image;

    // functions
    // ...
    };

    This used to work for a long time, but now, for some reason it's not and I have no idea if I even changed anything since it last worked. Actually, it works in my simulator, but it doesn't work on my phone anymore when I run it with debugging on my actual device; does that make a difference? My G1 is running v1.6 while I'm developing on v2.2
     
  6. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    change ClassName extends Activity

    to

    public class ClassName extends Activity.

    That's the first thing I see wrong.

    But if you are trying to start that activity, you need to use intents and startActivity(), not instantiate a new Class object
     
  7. markb

    markb Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    148
    Joined:
    Sep 14, 2009

    Sep 14, 2009
    148
    26
    63
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
    Did you try to create an instance using "new" to see if it would work?
    Have you verified that you can't create an instance of your class, no matter how you do it?

    What was the exception that you got?

    You have to declare your activity classes in the AndroidManifest.xml file.
    Have you done that?

    By the way, using class names that start with a lower case letter is not the usual convention, and may cause people to mis-read your code. If you create a class through Eclipse, it will actually give you this warning:

    "Type name is discouraged. By convention, Java type names usually start with an uppercase letter "

    Mark

    p.s.
    When you post code please use the CODE tags (the # button in the toolbar) to preserve layout, so it's easier for people to read.
     
  8. markb

    markb Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    148
    Joined:
    Sep 14, 2009

    Sep 14, 2009
    148
    26
    63
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
    Isn't that what he's already doing?

    Code (Text):
    1.  
    2. . . .
    3. i = new Intent(cApp.this, cAboutPage.class);
    4. startActivity(i);
    5. . . .
    6.  
     
  9. Leolicos

    Leolicos Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    54
    Joined:
    Jun 16, 2010

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    Yes I tested and verified that cAboutPage a = new cAboutPage() throws an exception. The exception is
    Thread [<3> main] (Suspended (exception VerifyError))
    Class.newInstance() line: 1472
    Instrumentation.newActivity(ClassLoader, String, Intent) line: 1097
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2316
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2417
    ActivityThread.access$2100(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 116
    ActivityThread$H.handleMessage(Message) line: 1794
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99
    Looper.loop() line: 123
    ActivityThread.main(String[]) line: 4203
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
    Method.invoke(Object, Object...) line: 521
    ZygoteInit$MethodAndArgsCaller.run() line: 791
    ZygoteInit.main(String[]) line: 549
    NativeStart.main(String[]) line: not available [native method]


    The activity is declared in my manifest.

    My cAboutPage worked before, I'm pretty sure I didn't change anything, I'm not sure why it's not working anymore.
     
  10. Leolicos

    Leolicos Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    54
    Joined:
    Jun 16, 2010

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    FYI, That stack is from startActivity(), not new cAboutPage
     
  11. markb

    markb Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    148
    Joined:
    Sep 14, 2009

    Sep 14, 2009
    148
    26
    63
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
    Someone had a similar sounding problem, with a similar stack trace here:
    Uncaught handler: thread main exiting due to uncaught exception (Android forum at JavaRanch)

    People link from there to other threads, and eventually you get to some possible explanations.

    People are suggesting that it's to do with using a version of the SDK that is incompatible with the phone. Which would explain why it works on the emulator, but not on the phone.


    And I just re-read your post, and should have spotted this bit straight away and flagged it up

    That'll be your problem. Things are backward compatible (a 2.2 phone can run 1.6 stuff), but not forward compatible. You can't run 2.2 stuff on a 1.6 device.

    Mark
     
  12. Leolicos

    Leolicos Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    54
    Joined:
    Jun 16, 2010

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    That's what I thought, but I wasn't sure if I was using any functionality that wasn't available in 1.6, and the reason why I didn't see that being a problem right away was because it worked before... I don't know why it would work before, but not now when it won't even let me create an instance of the class. It's just a simple Activity. Not even onCreate() is getting called...

    I'll look at that post though, thanks, do you have any idea why it would work at one point, but not now?
     
  13. Leolicos

    Leolicos Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    54
    Joined:
    Jun 16, 2010

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    Either way... if it is the API version for a fact, how do I tell my program to compile against v1.6 (API level 4) instead of v2.2
     
  14. Leolicos

    Leolicos Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    54
    Joined:
    Jun 16, 2010

    Jun 16, 2010
    54
    0
    15
    Senior Software Engineer
    Chicago, IL
    I found out what was the problem!! There's a function in ScrollView that isn't available in 1.6, I forgot the name, but it's something with setenableverticalbarfading(bool), and once I specified to build against 1.6, that function gave me an error. It works now :)
     
  15. markb

    markb Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    148
    Joined:
    Sep 14, 2009

    Sep 14, 2009
    148
    26
    63
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
    (earlier stuff about build target deleted, as you've already done it)

    I think it's a check that's done when the class is loaded.
    You're extending a 2.2 version of the Activity class, so when you create an instance of your class, you also create an instance of the Activity class.

    I don't know why it's effecting one of your activity classes in particular, or why it broke without you changing anything.

    Mark
     

Share This Page

Loading...