Question

Hi i always have this error: Android : Fatal Exception Main!! It's my first app and so i have several problems..i can't understand how android works :( :

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.example.toast.R;

public class MainActivity extends Activity {


private Button button;

public void onCreate(Bundle savedInstanceState) {

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

    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {

             Toast.makeText(getApplicationContext(), "Button is clicked",                    Toast.LENGTH_LONG).show();

          }
    });
}
}

This is the main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/show_toast"
    tools:ignore="HardcodedText" />

</LinearLayout>

This is the manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.toast"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Here's the logcat output:

enter image description here

Was it helpful?

Solution

First of all I think You had copied the Code Form Mykong

It's shown that you MainActivity.java is in package com.mkyong.android

But in your manifest file your main package name is com.example.toast .You haven't mention any about files in com.mkyong.android in your manifest file

Are you sure you are using tow package names ?? if so

So there are two options

OPTION 1


In your log it clearly says com.example.toast.MainActivity is not found

You must change the package of MainActivity.java to com.example.toast

<activity
    android:name="com.example.toast.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This will remove the RuntimeException

OPTION 2


The second option is to change in manifest file

<activity
    android:name="com.mkyong.android.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Note That if you application contains more than one package names you must specify the package name along with along with activity names in manifest file

eg : android:name="com.mkyong.android.MainActivity" where com.mkyong.android is package name and MainActivity is file name

I Prefer you to choose OPTION 1

To display you toast you can use

Toast.makeText(MainActivity.this, "Button is clicked", Toast.LENGTH_LONG).show();

Here MainActivity.this is the parameter for context

You can also use this

Toast.makeText(getApplicationContext(), "Button is clicked",Toast.LENGTH_LONG).show();

If you are beginner This Android Boot Camp Series Tutorial might help you

OTHER TIPS

Change Application context to your context and try

Toast.makeText(MainActivity.this, "Button is clicked", Toast.LENGTH_LONG).show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top