Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Issue with Activity Class in the Crystalball Manifest

There is a red line underneath the <activity line in the Crystalball Manifest. The error states, "Class referenced in the manifest, com.example.crystalball.MainActivity, was not found in the project or the libraries. I have come through the code so far and everything in the MainActivity looks exactly what is shown in the video. Here is a image of my Crystalball Manifest: https://www.evernote.com/shard/s124/sh/33a6d7ba-1735-44dc-b7e7-a4ee92f6fac9/5ecedf289a54c1e57843da8329e98fd4

Help?

5 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi Perry,

Can you try plugging in the full package name, like this:

<activity
    android:name="com.teamtreehouse.crystal.ball.MainActivity"

Okay so I added it and the error message went away. However, the crystalball app is now just crashing. I restarted the project and got as far as importing the textview and the button and using the findViewById() function. However, the viewText1 and Button ids did not show up. Eclipse added them to R.java to fix the problem. It still is crashing every time I start the emulator even though there are no error messages

09-03 14:28:17.943: D/AndroidRuntime(1226): Shutting down VM 09-03 14:28:18.013: W/dalvikvm(1226): threadid=1: thread exiting with uncaught exception (group=0x414c4700) 09-03 14:28:18.053: E/AndroidRuntime(1226): FATAL EXCEPTION: main 09-03 14:28:18.053: E/AndroidRuntime(1226): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.NullPointerException 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137) 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.app.ActivityThread.access$600(ActivityThread.java:141) 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.os.Handler.dispatchMessage(Handler.java:99) 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.os.Looper.loop(Looper.java:137) 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.app.ActivityThread.main(ActivityThread.java:5103) 09-03 14:28:18.053: E/AndroidRuntime(1226): at java.lang.reflect.Method.invokeNative(Native Method) 09-03 14:28:18.053: E/AndroidRuntime(1226): at java.lang.reflect.Method.invoke(Method.java:525) 09-03 14:28:18.053: E/AndroidRuntime(1226): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 09-03 14:28:18.053: E/AndroidRuntime(1226): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-03 14:28:18.053: E/AndroidRuntime(1226): at dalvik.system.NativeStart.main(Native Method) 09-03 14:28:18.053: E/AndroidRuntime(1226): Caused by: java.lang.NullPointerException 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.app.Activity.findViewById(Activity.java:1853) 09-03 14:28:18.053: E/AndroidRuntime(1226): at com.example.crystalball.MainActivity.<init>(MainActivity.java:17) 09-03 14:28:18.053: E/AndroidRuntime(1226): at java.lang.Class.newInstanceImpl(Native Method) 09-03 14:28:18.053: E/AndroidRuntime(1226): at java.lang.Class.newInstance(Class.java:1130) 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) 09-03 14:28:18.053: E/AndroidRuntime(1226): ... 11 more

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Okay, it looks like you might be using the wrong ID in the findViewById() method. Check out this excerpt from that log:

Caused by: java.lang.NullPointerException 09-03 14:28:18.053: E/AndroidRuntime(1226): at android.app.Activity.findViewById(Activity.java:1853) 09-03 14:28:18.053: E/AndroidRuntime(1226): at com.example.crystalball.MainActivity.(MainActivity.java:17)

That last part tells you the error is occurring on line 17 in your MainActivity.java file. Can you paste in all that code here? Make sure the ID matches the ID in your layout (you might need to check the XML view), and also make sure you are using the format R.id.textView1 (or whatever the appropriate name is).

In your earlier question you wrote 'viewText1'...did you maybe just switch up the parts of the ID?

Okay the format is correct.

Both lines say: TextView answerLabel = (TextView) findViewById(R.id.textView); Button getAnswerButton = (Button) findViewById(R.id.button1);

I removed the TextView line and it still crashed, but once I also removed the Button line, it is no longer crashing... Going to try to remove and re-add the textview and the button in the graphical layout and then add the views. But that's really helpful to know how to figure out what line is causing the crash. I'll let you know if it works

package com.example.crystalball;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

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

    TextView answerLabel = (TextView) findViewById(R.id.textView1);
    Button getAnswerButton = (Button) findViewById(R.id.button1);

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

All the ids are correct... what am I doing wrong?

http://blog.burnayev.com/2009/11/android-developer-tip-regenerating.html

does this have anything to do with what I'm seeing?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Sorry for my delayed response here! I've been on holiday for a long weekend. It is quite possible that something corrupted your R.java file. It looks like you are missing the import statement for your R class in your MainActivity file. It would look something like this:

import com.example.crystalball.R;

Here are a few forum posts that talk about issues with the R.java file:

No worries. I'm just getting frustrated and I don't want to give up but I'm at a lost. I added the import into my code and I followed a bunch of different methods to fix the corruption of my R.java. Each time they would update the R.id to reflect the ids I had created in my layout. But if I deleted or created another, it still would not regenerate. I even tried coding on another different computer that had never been used for coding. I had no problem with the R.java regenerating this time (using the Android Studio), but it is still crashing. Here is my code:

package com.example.crystalball;


import android.os.Bundle;
import android.app.Activity;
import com.example.crystalball.R;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

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

TextView answerLabel = (TextView) findViewById(R.id.textView1);

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

And here is the error readout:

09-10 18:37:31.780 279-499/system_process W/ActivityManager? Unbind failed: could not find connection for android.os.BinderProxy@412c4c48 09-10 18:37:31.780 667-667/com.android.exchange E/ActivityThread? Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce6028 that was originally bound here android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce6028 that was originally bound here at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) at android.app.ContextImpl.bindService(ContextImpl.java:1418) at android.app.ContextImpl.bindService(ContextImpl.java:1407) at android.content.ContextWrapper.bindService(ContextWrapper.java:473) at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) at android.os.AsyncTask$2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask.run(FutureTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:856) 09-10 18:37:31.790 667-667/com.android.exchange E/StrictMode? null android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce6028 that was originally bound here at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) at android.app.ContextImpl.bindService(ContextImpl.java:1418) at android.app.ContextImpl.bindService(ContextImpl.java:1407) at android.content.ContextWrapper.bindService(ContextWrapper.java:473) at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) at android.os.AsyncTask$2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask.run(FutureTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:856) 09-10 18:37:31.800 279-279/system_process W/ActivityManager? Unbind failed: could not find connection for android.os.BinderProxy@412c4498

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I've never seen this latest error, but it sounds like a problem with your emulator. What happens if you try the solutions in this StackOverflow question: http://stackoverflow.com/questions/13765122/android-emulator-spams-logcat-with-service-com-android-exchange-exchangeservice?

Followed the steps on the StackOver Flow. Restarted the emulator and also disabled the "Exchange Services." Back to this:

09-11 04:35:24.094    1315-1315/com.example.crystalball E/AndroidRuntime? FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity     ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at android.app.Activity.findViewById(Activity.java:1839)
    at com.example.crystalball.MainActivity.<init>(MainActivity.java:18)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1319)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
    ... 11 more
09-11 04:35:24.294      326-542/system_process W/ActivityManager? Force finishing activity com.example.crystalball/.MainActivity
09-11 04:35:24.294      326-542/system_process W/WindowManager? Failure taking screenshot for (328x546) to layer 22015
09-11 04:35:24.813      326-340/system_process W/ActivityManager? Activity pause timeout for ActivityRecord{40fc1030 u0 com.example.crystalball/.MainActivity}
09-11 04:35:25.154      326-343/system_process I/Choreographer? Skipped 47 frames!  The application may be doing too much work on its main thread.
09-11 04:35:25.164       36-184/? E/SurfaceFlinger? ro.sf.lcd_density must be defined as a build property
09-11 04:35:25.424        36-36/? E/SurfaceFlinger? ro.sf.lcd_density must be defined as a build property
09-11 04:35:28.471      428-430/com.android.phone D/dalvikvm? GC_CONCURRENT freed 366K, 16% free 2934K/3476K, paused 71ms+3ms, total 283ms
09-11 04:35:35.964      326-340/system_process W/ActivityManager? Activity destroy timeout for ActivityRecord{40fc1030 u0 com.example.crystalball/.MainActivity}

My code hasn't changed. Still pointing towards Line 18 which is:

TextView answerLabel = (TextView) findViewById(R.id.textView1);

The app won't crash when I leave it as TextView answerLabel; Not sure if that helps Although I am curious to what this means:

E/SurfaceFlinger? ro.sf.lcd_density must be defined as a build property. 

It wasn't there last time. Seems to refer to the resolution of the LCD... at least that's my guess

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Boy, this just isn't going to be easy for you, is it? :)

Next, can you paste in all your code from MainActivity.java (including the import statements) and activity_main.xml? You can also zip up your entire project and email it to help@teamtreehouse.com.

Any idea of what is wrong with my code?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Oh my goodness - this took me so long to troubleshoot! The error reporting in Android Studio is not as helpful as Eclipse. Not yet, at least.

Your TextView line is simply in the wrong place. Right now it's just floating in the nowhere space between methods. It needs to be inside the onCreate() method, meaning it needs to be before the right curly brace that matches the left curly brace that starts the block of code that makes up the method.

YOU ARE AWESOME!!!! Holy crap. Wow. It works. God, that was frustrating... thank you so much for all the help. This only reaffirms why I picked Treehouse over other sites. Thank you again!

Ha nope. But I'm invested at this point so I'm not ready to give up. I will email you the zip.