-2

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

nneonneo
  • 171,345
  • 36
  • 312
  • 383
Atlas91
  • 5,754
  • 17
  • 69
  • 141

2 Answers2

1

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

edwin
  • 7,985
  • 10
  • 51
  • 82
  • Oh God You are my hero ahah!Thx!! Only other quest.. I have to run a shell that i've created when the button is press.. how can i do it? – Atlas91 Apr 07 '13 at 15:48
  • ok, thx now i post another quest :) your solution is been perfect.thanks. and yes, the code is copied from Mykong:). – Atlas91 Apr 07 '13 at 17:56
  • anyway my shell is a script to free memory. this script goes in background and every 30min free memory ram. the problem is that i have to launch it with the terminal emulator..i wish that with a button in my app launch the script and with another stop it but i don't know how to do :( – Atlas91 Apr 07 '13 at 21:40
0

Change Application context to your context and try

Toast.makeText(MainActivity.this, "Button is clicked", Toast.LENGTH_LONG).show();
Roman C
  • 49,761
  • 33
  • 66
  • 176
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64