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

Code check, Android Login/Main Activity

Hi I am developing my own app using the tutorials here.

This code, like in the tutorial, is trying to bypass the Main Activity and go to Login; but I get an error that the application stops every time I run it, implying something is wrong with the code; I will continue to investigate but wanted to ask for help too...thanks so much!

public class MidwifeActivity extends Activity {

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

        //start with Login Activity
        Intent intent = new Intent(this, LoginActivty.class);

        //login is new task, clear former task from history (main)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

8 Answers

Hi Michael,

Apart from the pop-up in the emulator that unhelpfully tells you that the application stopped, can you show us the errors you will be getting in the Logcat part of Android Studio. In there, we may be able to identify precisely what's wrong and, more importantly, where!

Shout if you need a hand with that. Either on here or tweet me @OnlySteveH - I'll hear you!

Steve.

Hi, Thanks for your help. I just thought of LogCat after I posted (I developed an app using PhoneGap and used Eclipse..PhoneGap is probably not even around anymore).

Anyway, Logcat is stating this:

03-11 15:29:12.830 1367-1367/android.bignerdranch.com.mobilemidwife E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: android.bignerdranch.com.mobilemidwife, PID: 1367 java.lang.RuntimeException: Unable to start activity ComponentInfo{android.bignerdranch.com.mobilemidwife/android.bignerdranch.com.mobilemidwife.LoginActivty}:

Which leads me to think it's something to do with the LoginActivity?

Here that is (below); I will continue to troubleshoot; fairly new to this, though I read through the BigNerdRanch Guide to Android.

And, yes, I am making an app for midwives...

Thanks for any help you can provide

Michael

package android.bignerdranch.com.mobilemidwife;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;


public class LoginActivty extends ActionBarActivity {

    protected TextView mRegisterTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_activty);

        mRegisterTextView = (TextView)findViewById(R.id.register);
        mRegisterTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivty.this, LoginActivty.class);
                startActivity(intent);
            }


        });

        ActionBar actionBar = getActionBar();
        actionBar.hide();

        TextView textView = (TextView)findViewById(R.id.title);
        Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/desyrel.ttf");
        textView.setTypeface(typeFace);
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

I'll edit that to make the code a little clearer then have a look through to see what might be the problem.

Steve.

Is there code anywhere that is expecting LoginActivty to be spelt "correctly" - that might cause an issue.

And what is the onClickListener in onCreate doing? The intent seems to make it stay put.

(I'll keep editing/adding as I think of stuff!)

Hi I updated the code you mentioned; onClickListener is supposed to be:

mRegisterTextView = (TextView)findViewById(R.id.register);
        mRegisterTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }


        });

I'm moving on to trying Parser, and need to fix some things there; but will try to run the app again.

Thanks so much for your help

Michael

Are you using Parse.com for your back-end?

Hi, Yes I am...but I was getting this error before I started using Parse.

This is for a grad design thesis project...I appreciate your help!

Michael

What error is persisting still? Did amending that code not fix the activity showing up?

Steve.

the error still persists.

Not sure why

Thanks for your help!

Michael

Can you put your code into a Github repo - I can then have a look through it and see what might be missing.

Cheers,

Steve.

Hi Got it working...thanks so much!

Michael