Corey Jacobb
Corey Jacobb

Reputation: 29

App crashes when changing intent

I'm new to android and the problem feels simple, but I can't place it. I tried to look in logcat, but I can't follow.

Here is the main activity which loads just fine.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity  {

Intent intent;
Button newsButton; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}


@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;
}    

public void sendMessage(View view) {
    Intent intent = new Intent(this, NewsActivity.class);
    startActivity(intent);
}

}  

Here is the corresponding xml:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:orientation="vertical" >

    <Button
        android:id="@+id/newsButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="104dp"
        android:onClick="@layout/news_activity"
        android:text="News" />

    <Button
        android:id="@+id/contactButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Contact Us"
        android:onClick="@layout/contact_activity" />

    <Button
        android:id="@+id/findUs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Social Media"
        android:onClick="@layout/social_activity" />

    <Button
        android:id="@+id/fundButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fundraising" />
</LinearLayout>

<TextView
    android:id="@+id/titleName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:text="Send the Scots to Scotland"
    android:textSize="24sp" />

</RelativeLayout>

The crash happens on the API 14 device I debug with. After clicking any one of my buttons to change to a new activity that looks like this :(which are all essentially the same as the one below)

package com.gconcode.scotstoscotland;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;

public class NewsActivity extends Activity {

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


}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-  vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Sorry I removed the logcat, I wasn't sure if I posted it right. Didn't want too cause the post to be inconvenient thanks for the help!

Upvotes: 0

Views: 571

Answers (1)

codeMagic
codeMagic

Reputation: 44571

I think you misunderstand android:onClick. It looks like you are expecting it to load a certain layout file when you click the Button but that is not how it works. The name you give android:onClick should be the name of a function which doesn't return anything and accepts a View as its one and only parameter. It looks like you pulled that from the example in the docs with

public void sendMessage(View view) {
    Intent intent = new Intent(this, NewsActivity.class);
    startActivity(intent);
}

There are different ways you could do this. Since you already have the android:onClick and you have a method in your Activity, you could change that to look like

android:onClick="sendMessage"

then add a switch statement to that method to open the correct Activity. So something like

public void sendMessage(View view) 
{
    switch (view.getId())   // get the id of the View clicked
    {
         case (R.id.newsButton):
            Intent intent = new Intent(this, NewsActivity.class);
            startActivity(intent);
            break;
        case (R.id.contactButton):
            // assuming ContactActivity.java is an Activity name
            Intent intent = new Intent(view.getContext(), ContactActivity.class);
            startActivity(intent);
            break;
       ...
    }
}

This answer shows a little different way to do the Intent so you cut down on some of the reusable code. If it looks too complicated then don't worry about it until you have a better understanding but just an idea.

Upvotes: 2

Related Questions