Android app crashes with "Unfortunately, <appname> has stopped" as soon as I try to launch an Activity

StackOverflow https://stackoverflow.com/questions/21586880

سؤال

Here is the MainActivity code:

package com.vanitfyf.karko;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {



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


        TextView textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setTextColor(Color.BLUE);
        Button button1 = (Button) findViewById(R.id.button1);


        button1.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
            {
                Toast.makeText(MainActivity.this, "entering debug mode", Toast.LENGTH_LONG).show();
                startActivity(new Intent(MainActivity.this, SecondActivity.class ));
            }

        });

    Button button2 = (Button)findViewById(R.id.button2);
    button2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "entering normal mode", Toast.LENGTH_LONG).show();
                startActivity(new Intent(MainActivity.this, ThirdActivity.class));

            }
        });
    }





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

}

Here is the SecondActivity code:

package com.vanitfyf.karko;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;

public class SecondActivity extends Activity
{

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




protected void show(Context context) {
    Toast.makeText(context, "welcome", Toast.LENGTH_LONG).show();

    }


}

And here's the ThirdActivity code:

package com.vanitfyf.karko;

import android.app.*;
import android.os.*;
import android.widget.*;

public class ThirdActivity extends Activity { 

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

    TextView textview2 = (TextView) findViewById(R.id.textview2);
    textview2.setText("Welcome to normal mode");

    }
  }

The app crashea as soon as I try to press the button that opens the ThirdActivity. The third activity does not display. I have been pulling my hair out in frustration during a plane trip trying to find out what's wrong but I still haven't got it. The Activities all reference the correct layout .xmls by the way.

Any pointers? I'm new at this (obviously) so I'd appreciate any help!

هل كانت مفيدة؟

المحلول

Based on the code you have provided, there are a couple intuitive reasons why your app would be crashing under the circumstances you have described.

Make sure you declare your third Activity in the Manifest, otherwise you will receive a null pointer exception (which could cause the app to crash) upon launching the Activity. This is a requirement for all Activities within your application. See more information here: http://developer.android.com/guide/topics/manifest/manifest-intro.html.

Also, verify that the layout for your third Activity, "activitythird", is a valid resource name and contains the TextView that you are referencing in the third activity, "textview2". If using an IDE such as Eclipse ADT or Android Studio, you should receive an indication that the resource is not found; if not automatically, at least at compile time. The same applies for the button in which initiates the launching of the third activity, "button2".

نصائح أخرى

If it is not declared in the manifest, it also shows up in LogCat

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top