JAVA - Android error: The application has stopped unexpectedly please try again - android

I made an app which is just working fine. But when i click on "cancel" or "ok" button when i run the program, instead of linking me to result.java, the simulator give me "The application has stopped unexpectedly please try again" Here's the code
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.database.CursorJoiner.Result;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
public class lib_main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onAddClick (View botton){
AutoCompleteTextView title = (AutoCompleteTextView)findViewById(R.id.Title);
AutoCompleteTextView author = (AutoCompleteTextView)findViewById(R.id.Author);
AutoCompleteTextView isbn = (AutoCompleteTextView)findViewById(R.id.ISBN);
Intent intent = new Intent();
intent.setClass(this,Result.class);
intent.putExtra("Title", title.getText().toString());
intent.putExtra("Author", author.getText().toString());
intent.putExtra("ISBN", isbn.getText().toString());
startActivity(intent);
}
public void onCancelClick(View botton){
Intent intent = new Intent();
intent.setComponent(new ComponentName(this,Result.class));
intent.putExtra("Cancel","Cancel");
startActivity(intent);
}
}
this is Result.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Result extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView resultText=(TextView)findViewById(R.id.resultText);
Bundle bundle =getIntent().getExtras();
if(bundle.getString("Cancel")!= null)
{resultText.setText("operation cancel");}
else
{ String book =bundle.getString ("Title");
String title =bundle.getString ("Author");
String isbn =bundle.getString ("ISBN");
resultText.setText(
getString (R.string.resultOk)+""+book+""+ title+""+isbn);
}
}
}
the error log write "No command output when running:'am start-n com.Library.doc/com.Library.doc.lib_main-a android.intent.action.MAIN-c android.intent.category.LAUNCHER' in device emulator-5554"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Library.doc"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".lib_main"
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=".Result"
android:label="#string/result">
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
Can anyone help me with this problem?

Quick question, have you registered both Activities? If you only provide for one in your manifest file you're going to get this error every time you attempt to do something involving the unregistered activity.

Related

Unable to find explicit activity class exception even if the class is declared in the projects manifest

I transformed my project into a library. This project just displays a blank activity. I imported this library into another project where I want to call it thru the projects main activity but I keep on getting the error below even if I already declared the activity inside the projects manifest.
Unable to find explicit activity class {com.package.name/com.package.name.BatibotActivity}; have you declared this activity in your AndroidManifest.xml?
This is my project manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pak.paks.pocket" >
<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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.package.name.BatibotActivity"
android:screenOrientation="portrait" >
</activity>
</application>
</manifest>
What am I doing wrong?
edit:
this is my BatibotActivity
package com.package.name;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class BatibotActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_batibot);
}
}
EDIT2
This is the class that is calling the BatibotActivity
package pak.paks.pocket;
import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void submitBtn(View view) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("com.package.name",
"com.package.name.BatibotActivity"));
startActivity(intent);
}
}
By the log message, you are trying to open com.package.name/com.package.name.BatibotActivity, but the Activity registered on AndroidManifest.xml is pak.paks.pocket/com.package.name.BatibotActivity.
The first parameter for ComponentName is the package name. This is the correct code:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("pak.paks.pocket", "com.package.name.BatibotActivity"));
startActivity(intent);
Please check your manifest tag's package attr.
<manifest package="com.package.name">
<application>
...
</application>
</manifest>

Cannot load new blank activity - android tutorial

I am a new android developer and encountered a problem while following the tutorial on Android's site - http://developer.android.com/training/basics/firstapp/starting-activity.html
My program loads on the emulator fine, but when you type something into the EditText and then hit the send button, a window pops up saying "App has stopped working". It seems as though my new activity isn't being created and I'm not sure as to why. I've scoured the web (and my code) to see if I could get a solution but did so to no avail. Any help would be appreciated.
The code for my first activity:
package com.example.appli;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testlayout);
//////Button myButton = (Button) findViewById(R.id.my_button); //Told in tutorial to put this in onCreate
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when the user selects the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
The code for my second activity (the one being called)
package com.example.appli;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get message from intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
The Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appli"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.example.myapp.DisplayMessageActivity" />
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In AndroidManifest File you have declared incorrect package
change
<activity android:name="com.example.myapp.DisplayMessageActivity" />
to
<activity android:name="com.example.appli.DisplayMessageActivity" />
or just
<activity android:name=".DisplayMessageActivity" />
you have declared only one Activity that's also wrong package name
already your manifest has the package name
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appli"
so just declare like this
<activity android:name=".DisplayMessageActivity" />
and you need to add this in your manifest also
<activity android:name=".MainActivity />
remove the target SDK version that you mentioned
android:targetSdkVersion="15"
Try this
<activity android:name=".DisplayMessageActivity"/>
Instead of
<activity android:name="com.example.myapp.DisplayMessageActivity" />
in your Manifest.xml file.
You has defined the package package com.example.appli; in your activities.
Why have you defined you activity in manifest like <activity android:name="com.example.myapp.DisplayMessageActivity" />?
Replace this line with
<activity android:name=".DisplayMessageActivity" />.

Android Step 1 from Root Developer Page Activity not Working

I attempted to run "Clean Code" according to this link, however nothing is happening. From being a Renegade developer my intuition says "Button onClick" is missing but I could be wrong. Here is my code written according to what the instructions are:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class BuildingaSimpleUserInterfaceApi8Activity extends Activity {
public final static String EXTRA_MESSAGE = "com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/**This is how I work with Buttons and the Next Activity, however this method IS NOT defind
* in the very first Android Tutorial for sending information onClick.
Button bButtonone = (Button) findViewById(R.id.button_send);
bButtonone.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY"));
}
});
}
*/
/** Called when the user selects the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.developer.android.com_training_basics_firstapp_building_ui"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BuildingaSimpleUserInterfaceApi8Activity"
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.developer.android.com_training_basics_firstapp_building_ui.DisplayMessageActivity" />
</application>
</manifest>
It looks like you forgot the line to start the activity:
public void sendMessage(View view) {
...
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent); // add me!
}
I read through the code at the site listed and you have missed one line:
startActivity(intent);
at the end od sendMessage.

Android:Broadcast demo not working

I am new to android, i am trying a broadcast demo, I gave my best effort by reading the documentation but its not working. Please have a look at my code:
BroadcastDemoActivity.java
package com.broadcastdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class BroadcastDemoActivity extends Activity {
/** Called when the activity is first created. */
public static final String PUBLIC_HOLIDAYS = "com.paad.action.PUBLIC_HOLIDAYS";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(getIntent());
}
}
Receive.java
package com.broadcastdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Receive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String message = intent.getStringExtra("Holiday");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcastdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BroadcastDemoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".Receive">
<intent-filter>
<action android:name="com.paad.action.PUBLIC_HOLIDAYS"/>
</intent-filter>
</receiver>
</application>
</manifest>
I know that i am missing something which i am not aware of, please help.
I believe your problem is in the call to sendBroadcast.
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(getIntent());
You are not sending the intent that you construct, you're sending the intent returned from getIntent(), which will be the intent that the activity was started with.
It should be
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(intent);

Android. Broadcast Receiver

I have some trouble while try using broadcast receiver.
Target:
I have three app which will work next schema
1. First - is broadcast receiver app which will write some data to database when it will get a message.
2. Second - is app android which will send some intent with data which must be saved in database.
3. Third - is widget in home screen which will also send some intent with data which must be saved in database.
So, I make three app on eclipse.
1. BroadcastReceiverExample - broadcast receiver it has next files
package com.test.receive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class SimpleReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "service get started action", Toast.LENGTH_LONG).show();
Log.e("START","START");
}
}
and the manifest file source
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:enabled="true" android:name=".receive.SimpleReceiver" android:exported="false">
<intent-filter android:priority="999">
<action android:name="com.test.SIMPLE_TEST_SERVICE"></action>
</intent-filter>
</receiver>
</application>
</manifest>
also I create App project (BroadcastSenderExample) in Eclipse
and it has file with next sender code
package com.test.sender;
import com.test.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BroadcastSenderExample extends Activity {
public final static String ACTION="com.test.SIMPLE_TEST_SERVICE";
public final static String TYPE="type";
public final static int START=1;
public final static int STOP=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStart=(Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent bcIntent=new Intent(ACTION);
sendBroadcast(bcIntent);
}
});
btnEnd=(Button)findViewById(R.id.btnEnd);
btnEnd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent bcIntent=new Intent(ACTION);
sendBroadcast(bcIntent);
}
});
}
private Button btnStart=null;
private Button btnEnd=null;
}
Then I install first app on device (and emulator try too), and install second app.
And then second app run intent call nothing happen.
What am I doing wrong?
I make two projects with next code
Project one wBRReceiver
File WBRReceiver.java
package com.x.brreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class WBRReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.i("THIS IS A TEST RECEIVER","THIS IS A TEST RECEIVER");
Toast.makeText(arg0, "this is a test receiver", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.x.brreceiver"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="WBRReceiver">
<intent-filter>
<action android:name="com.x.START"></action>
</intent-filter>
</receiver>
</application>
</manifest>
And project two wBRSender
File WBRSenderActivity.java
package com.x.brsender;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class WBRSenderActivity extends Activity {
private String ACTION_NAME="com.x.START";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent brr=new Intent(ACTION_NAME);
//I can't use this
//brr.setClass(this, WBRReceiver.class);
//Because i just don't have this class in this case
sendBroadcast(brr);
}
}
And manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.x.brsender"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".WBRSenderActivity"
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>
And then I install first app onto emulator, and then run second app. And it works.
Did you look at the logcat output? There's a very good chance it tells you exactly what's wrong.
Without staring at your code too much, it seems that your manifest is broken. In your receiver, you state the android:name is ".receive.SimpleReceiver"... this value (when starting with a .) is not simply "the part that follows the Android package name) -- though it works out that way most of the time. In your case, your Android package is "com.test" however the package containing your receiver is "com.test.receive.SimpleReceiver" and its Java package is "com.test.receive". Try changing your android:name to "com.test.receive.SimpleReceiver".

Categories

Resources