Aaron Stephens
Aaron Stephens

Reputation: 25

App Crashes Right Away

I'm extremely new to Android so chances are I'm probably not doing something simple that's giving me this problem. As soon as I try to run my app, it shuts down immediately, I have no idea why.

Here's my Java code:

package com.example.androidside;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
    public static final String TAG = "MyActivity";
    ActivityManager am;
    int s;
    List<ActivityManager.RunningAppProcessInfo> l;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        l = WhatchyaDoin();
        s = l.size();
        for(int i = 0; i < s; i++) {
            Log.d(TAG, "" + l.get(i));
        }
    }
    public List<ActivityManager.RunningAppProcessInfo> WhatchyaDoin() {
        return(am.getRunningAppProcesses());
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

If anyone could help me out it would be much appreciated, thanks.

Upvotes: 0

Views: 90

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234795

You aren't initializing am and are getting a NullPointerException when you call WhatchayDoin() from onCreate().

You should learn how to read the logcat output from your program. (In Eclipse, you can open the logcat view by going to Window->Show View->Other and then navigating to Android->LogCat. That will often tell you what's going on. If you can't figure it out, then, when you post questions about Android crashes, always include the relevant logcat output.

You can initialize am in onCreate() with:

am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

Upvotes: 1

Related Questions