Android : Fatal Exception Main toast - android

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:

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

Change Application context to your context and try
Toast.makeText(MainActivity.this, "Button is clicked", Toast.LENGTH_LONG).show();

Related

Fatal Exception applicationId is null Parse

This is my second question in SO, I have a problem about Fatal Main error. Before I ask it, I found some other problem in SO, but what I found is about Facebook app
This is my error
java.lang.RuntimeException: applicationId is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.
But, I have a class that declared it
package com.example.michael.eksperimen6chatting;
import android.app.Application;
import com.parse.Parse;
/**
* Created by Michael on 03/12/2015.
*/
public class ChattApp extends Application
{
#Override
public void onCreate()
{
super.onCreate();
Parse.initialize(this, "urkcFwvUmgSYvuOFTInJmkA3iWv0ArV3XT128TNb", "WvBC0cnGXHkVhTBbkFwLyxGDRgqR6qC8ft7DIleT");
}
}
and this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.michael.eksperimen6chatting" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
</activity>
<activity android:name=".UserList"></activity>
<activity android:name=".Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Chat">
</activity>
</application>
</manifest>
Did I miss something here, can anyone help me? Gratia
Specifying ChattApp Application class name in your AndroidManifest.xml's <application> tag.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:name="com.example.michael.eksperimen6chatting.ChattApp">
....
</application>
import com.parse.Parse;
import android.app.Application;
public class ChattApp extends Application {
#Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
}
}
AndroidManifest.xml
<application
android:name="com.example.michael.eksperimen6chatting.ChattApp"
.
.
.
</application>

Android: onBackPressed() causes application to crash

I am attempting to use the onBackPressed() method provided by android in order to
The class that I am using the onBackPressed() method:
import com.interviewme.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Preparing extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preparation);
}
#Override
public void onBackPressed() {
Intent t = new Intent(this, Menu.class);
startActivity(t);
}
}
The part of the manifest file where I am trying to go back to:
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The full manifest file of the app:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.interviewme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.interviewme.Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.interviewme.Play"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.PLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.interviewme.Result"
android:label="#string/title_activity_result" >
</activity>
<activity
android:name=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.interviewme.menu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.interviewme.About"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.interviewme.InterviewTips"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.interviewme.Preparing"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
The error log from logcat:
05-09 08:09:43.420: E/AndroidRuntime(1741): FATAL EXCEPTION: main
05-09 08:09:43.420: E/AndroidRuntime(1741): Process: com.interviewme, PID: 1741
05-09 08:09:43.420: E/AndroidRuntime(1741): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.interviewme/android.view.Menu}; have you declared this activity in your AndroidManifest.xml?
05-09 08:09:43.420: E/AndroidRuntime(1741): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
05-09 08:09:43.420: E/AndroidRuntime(1741): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
http://androidmanifest.xml/
androidmanifest.xml
I notice that It states ActivityNotFound, however this Activity is definitely within my app, is it named incorrectly in manifest?
its picking android default Menu
remove this line
import android.view.Menu;
and import your class or change your class name Menu to another name(eg. Menu1)
You imported android.view.Menu. Try writing out com.interviewme.Menu instead. Also use better names for your activities. Menu is kind of ambiguous.
Error showing because below:-
import android.view.Menu;
Intent t = new Intent(this, Menu.class);
startActivity(t);
Your code pick view.Menu.
you have to use :-
Intent t = new Intent(this, com.interviewme.Menu.class);// where menu class here
startActivity(t);

my first android app doesn't running after successful installation

I am making an android app. the first launcher activity code is :
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
public class FirstPage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sp = getSharedPreferences("STATE",0);
String x = sp.getString("typeOfUser","");
if(x==""){
setContentView(R.layout.activity_first_page);
Intent intent = new Intent(this,LoginOrRegister.class);
startActivity(intent);
}
else {
setContentView(R.layout.lat_lon);
}
}
}
Manifest file is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:maxSdkVersion="19" android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test.FirstPage"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginOrRegister" />
<activity android:name="Registration"></activity>
<activity android:name="SignIn"></activity>
</application>
</manifest>
But when running this app. there is error: :
No command output when running: 'am start -n com.example.test/com.example.test.FirstPage -a android.intent.action.MAIN -c android.intent.category.LAUNCHER' on device emulator-5554
this is my stack trace:
com.android.ddmlib.ShellCommandUnresponsiveException
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:430)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:347)
at com.android.ddmlib.Device.executeShellCommand(Device.java:584)
at com.android.ide.eclipse.adt.internal.launch.ActivityLaunchAction.doLaunchAction(ActivityLaunchAction.java:67)
at com.android.ide.eclipse.adt.internal.launch.ActivityLaunchAction.doLaunchAction(ActivityLaunchAction.java:109)
at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.doLaunchAction(AndroidLaunchController.java:1293)
at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.doLaunchAction(AndroidLaunchController.java:1305)
at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.launchApp(AndroidLaunchController.java:1277)
at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.simpleLaunch(AndroidLaunchController.java:913)
at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.continueLaunch(AndroidLaunchController.java:755)
at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.launch(AndroidLaunchController.java:575)
at com.android.ide.eclipse.adt.internal.launch.LaunchConfigDelegate.doLaunch(LaunchConfigDelegate.java:330)
at com.android.ide.eclipse.adt.internal.launch.LaunchConfigDelegate.launch(LaunchConfigDelegate.java:246)
at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:855)
at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:704)
at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:1047)
at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1251)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
Could be a duplicate of one of these questions, which have to do with the same ShellCommandUnresponsiveException exception shown in the stack trace:
Android CTS is showing ShellCommandUnresponsiveException on emulator
Android App Won't Display in Emulator

Adding Shared Library Project Android

I'm struggling with adding a library to my project. I've been following a few other SOs as well as the tictactoemain/lib sample Android provides, but I'm still getting a "unable to find explicit activity class" error. The library package I included showing up under Android Dependencies is com.example.surveymetest. I suspect the issue is how I'm calling/defining the activity in the manifest but I can't seem to get it right. Any ideas where I'm going wrong?
Here's my manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.surveymedemo.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>
<activity
android:name="com.example.surveymetest.SurveyActivity"
android:label="#string/app_name" >
</activity>
<activity android:name="com.example.surveymetest.TakeSurveyActivity" >
</activity>
<provider
android:name="com.example.surveymetest.SurveyMeContentProvider"
android:authorities="io.surveyme.ContentProviders.SurveyMeContentProvider"
android:exported="true" >
</provider>
</application>
</manifest>
Calling the Activity:
package com.example.surveymedemo;
import com.example.surveymetest.StartSurveyActivity;
import com.example.surveymetest.SurveyMe;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, StartSurveyActivity.class);
startActivity(i);
}
#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;
}
}
Add:
<activity
android:name="com.example.surveymetest.StartSurveyActivity"
android:label="#string/app_name" >
</activity>
to your manifest and you should be good.
(you only defined com.example.surveymetest.SurveyActivity)

Unable to instantiate activity ComponentInfo java.lang.ClassNotFoundException: util.kalyan.HelloGoogleMapsActivity

I have included all the necessary fields correctly.
But still, I am not able to launch the Google map on my emulator.
Checked the internet permissions also.
Getting error like:
E/AndroidRuntime(2064): java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{util.kalyan/util.kalyan.HelloGoogleMapsActivity}:
java.lang.ClassNotFoundException: util.kalyan.HelloGoogleMapsActivity
Please find below my code.
HelloGoogleMapsActivity.java
package util.kalyan;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import util.kalyan.R;
public class HelloGoogleMapsActivity extends MapActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
#Override
protected boolean isRouteDisplayed()
{
return false;
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="util.kalyan"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<uses-library android:name="com.google.android.maps"></uses-library>
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" >
<activity android:name=".HelloGoogleMapsActivity" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses- permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Any one please help me in this at the earliest.
Thanks in advance.
Change the android:name in activity definition
from
<activity android:name=".HelloGoogleMapsActivity" ../>
to
<activity android:name="util.kalyan.HelloGoogleMapsActivity" ../>
You emulator target should be one that includes Google APIs (Google Inc.).
Right click on your HelloGoogleMapsActivity class > BuildPath > Exclude Then Right click on your HelloGoogleMapsActivity class > BuildPath > Include your class again

Categories

Resources