Android app suddenly stops - java

I am learning the basics of android programming. Now I have made a Life Cycle test. The list activity works fine, but when I attempt to open the Life Cycle test, the app stops and closes. I have got the same problem on several devices. These are my codes:
(Manifest)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.badlogic.androidgames"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".AndroidBasicsStarter"
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>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>
(AndroidBasicsStarter.java)
package com.badlogic.androidgames;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AndroidBasicsStarter extends ListActivity {
String tests[] = { "LifeCycleTest", "SingleTouchTest", "MultiTouchTest", "KeyTest",
"AccelerometerTest", "AssetsTest", "ExternalStorageTest", "SoundPoolTest",
"MediaPlayerTest", "FullScreenTest", "RenderViewTest", "ShapeTest", "BitmapTest",
"FontTest", "SurfaceViewTest"
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, tests));
}
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);
String testName = tests[position];
try {
Class clazz = Class
.forName("com.badlogic.androidgames." + testName);
Intent intent = new Intent(this, clazz);
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
(LifeCycleTest.java)
package com.badlogic.androidgames;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class LifeCycleTest extends Activity {
StringBuilder builder = new StringBuilder();
TextView textView;
private void log(String text) {
Log.d("LifeCycleTest", text);
builder.append(text);
builder.append('\n');
textView.setText(builder.toString());
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText(builder.toString());
setContentView(textView);
log("created");
}
#Override
protected void onResume() {
super.onResume();
log("resumed");
}
#Override
protected void onPause() {
super.onPause();
log("paused");
if (isFinishing()) {
log("finishing");
}
}
}
I use Eclipse on Windows 7. Thank you in advance!

Add your LifeCycleTest activity in AndroidManifest.xml
<activity
android:name=".LifeCycleTest"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>

Include both your activities inside your Manifest file. It should look something like this.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".AndroidBasicsStarter"
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=".LifeCycleTest"
android:label="#string/app_name" >
</activity>
</application>

Related

Android 8.1 : broadcast receive does not work when I restart the smartphone

I am designing an android application in java and I would like it to launch when the smartphone starts up I of course registered a BroadcastReceiver for the BOOT event of my phone as explained here but it does not work on Android 10 if not for info it worked on Android 4.1.
Can you help me please ?
Here is my code:
Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.openclassrooms.fr.premierprojet">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".PremiereActivite">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".StartAppOnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>
Classe Main :
package com.openclassrooms.fr.premierprojet;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class PremiereActivite extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Classe Diffuseur :
package com.openclassrooms.fr.premierprojet;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartAppOnBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent i = new Intent(context, PremiereActivite.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Cordially.

Start an application after Android boot

I need to know how to make an application that I created in Android and made in Android Studio runs automatically when the device boots.
The only thing I have found is this guide https://codigofacilito.com/articulos/auto-ejecucion-de-una-aplicacion-al-inicio-de-un-depositivo-con-android but it does not run when the device starts.
ANDROID MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.resolucion.pruebaauto">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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>
<receiver android:enabled="true"
android:name=".Auto"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>
BROADCAST RECEIVER
package com.resolucion.pruebaauto;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Auto extends BroadcastReceiver{
public void onReceive(Context context,Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
MAINACTIVITY
package com.resolucion.pruebaauto;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

ActivityNotFoundException: Unable to find explicit activity class {co.edu.unimagdalena.projecto/co.edu.unimagdalena.projecto.informacion2}

i have one package with two class
MainActivity:
package co.edu.unimagdalena.projecto;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
String[] info;
private EditText nombre;
private EditText apellido;
private EditText email;
private EditText telefono;
private Button enviar;
final static String INFO = "co.edu.unimagdalena.projecto.informacion2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nombre=(EditText)findViewById(R.id.EditTextNombre);
apellido=(EditText)findViewById(R.id.EditTextApellidos);
email=(EditText)findViewById(R.id.EditTextCorreo);
telefono=(EditText)findViewById(R.id.EditTextTelefono);
enviar = (Button)findViewById(R.id.BtnEnviar);
enviar.setOnClickListener(this);
info=new String[4];
}
public void onClick (View v){
Intent intent = new Intent (this, informacion2.class);
intent.setClassName("co.edu.unimagdalena.projecto","co.edu.unimagdalena.projecto.informacion2");
startActivity(intent);
}
public void pasarActidadInfo (View v){
info [0] = nombre.getText().toString();
info [1] = apellido.getText().toString();
info [2] = email.getText().toString();
info [3] = telefono.getText().toString();
Intent act = new Intent (this, informacion2.class);
act.putExtra(INFO, info);
startActivity(act);
}
and other class call it informacion2:
package co.edu.unimagdalena.projecto;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.TextView;
import co.edu.unimagdalena.projecto.MainActivity;
public class informacion2 extends Activity {
String [] info;
TextView nombre,apellido,email,telefono;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.informacion2);
nombre = (TextView) findViewById(R.id.mostrarNombre);
apellido= (TextView) findViewById(R.id.mostrarApellido);
email = (TextView) findViewById(R.id.mostrarEmail);
telefono = (TextView) findViewById(R.id.mostrarTelefono);
Intent men = getIntent();
info = men.getStringArrayExtra(MainActivity.INFO);
nombre.setText(info[0]);
apellido.setText(info[1]);
email.setText(info[2]);
telefono.setText(info[3]);
}
}
and this is the AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.edu.unimagdalena.projecto">
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<activity
android:name=".informacion2"
android:label="#string/app_name">
</activity>
</manifest>
**I already trying everything and the error is not corrected, i am new in android studio
I can fix the error but now android studio give me this error
java.lang.RuntimeException: Unable to start activity ComponentInfo{co.edu.unimagdalena.projecto/co.edu.unimagdalena.projecto.informacion2}: java.lang.NullPointerException: Attempt to read from null array**
Try including other activity inside application in manifest file like this
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<activity
android:name=".informacion2"
android:label="#string/app_name">
</activity>
</application>
setClassName takes a Package Context as first param setClassName(Context packageContext, String className):
In Android manifest
<activity android:name="co.edu.unimagdalena.projecto.informacion2">
And while starting the activity from your main activity
intent.setClassName("co.edu.unimagdalena.projecto","co.edu.unimagdalena.projecto.informacion2");
You are getting NullPointerException inside second activity, when you try to get values of Array. And you are filling array inside pasarActidadInfo method of first activity. The problem is that you are not calling pasarActidadInfo from anywhere after filling array in your first activity. So,your array would be null.
Try calling pasarActidadInfo method when you want to start new activity.
May be in onClick of button.
Always declare all android components (activities, service, receivers) inside the application tag / block and other configurations will be defined outside of application tag/block and inside manifest tag / block in Androidmanifest.xml.
Copy and paste this code in your manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.edu.unimagdalena.projecto">
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<activity
android:name=".informacion2"
android:label="#string/app_name">
</activity>
</application>
</manifest>

Send Array to Different Application

I would like to send an array in different application.
So In the first application I have MainActivity. The Activity will start an intent. When I'm trying to make an intent, I found a problem. The problem is ShowArrayActivity "cannot be resolved to a type". Actually ShowArrayActivity class is in another apppication. I have tried to add project in build path, but I have an error message said that a cycle was detected in the build path of project 'sendIntent'..
package com.example.sendintent;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import com.example.sendintent.DisplayMessageActivity;
public class MainActivity extends Activity {
public static final String ARRAYS_COUNT = "com.example.sendintent.MainActivity.ARRAYS_COUNT";
public static final String ARRAY_INDEX = "com.example.sendintent.MainActivity.ARRAY_INDEX";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void sendMessage(View view)
{
final String data[][] = new String[][]
{{"Car","75","loaded"},
{"Motor","25","loaded"},
{"Truck","30","loaded"},
{"Bike","40","loaded"},
{"Boat","10","loaded"}};
Bundle bundle = new Bundle();
int count = data.length;
bundle.putInt(ARRAYS_COUNT, count);
for (int i = 0; i < count; i++)
bundle.putStringArray(ARRAY_INDEX + i, data[i]);
Intent intent = new Intent(this, ShowArrayActivity.class); ---->ERROR
intent.putExtras(bundle);
startActivity(intent);
}
}
In Manifest I have declare
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendintent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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.example.sendintent.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.sendintent.DisplayMessageActivity"
android:parentActivityName="com.example.sendintent.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.sendintent.MainActivity" />
</activity>
<service android:name="com.example.sendintent.MainActivity" />
<receiver android:name="com.example.receiver.MainActivityReceiver">
<intent-filter>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_COUNT"/>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_INDEX"/>
</intent-filter>
</receiver>
</application>
</manifest>
In another application, I have declare the receiver for the intent. It called MainActivityReceiver
package com.example.receiver;
import java.util.ArrayList;
import com.example.sendintent.MainActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MainActivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
intent.setClass(context, MainActivity.class);
context.startService(intent);
}
}
I have another class called ShowArrayAcivity:
package com.example.receiver;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class ShowArrayActivity extends Activity {
public String[][] data;
public static final String ARRAYS_COUNT = "com.example.sendintent.MainActivity.ARRAYS_COUNT";
public static final String ARRAY_INDEX = "com.example.sendintent.MainActivity.ARRAY_INDEX";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
int count = bundle.getInt(ShowArrayActivity.ARRAYS_COUNT, 0);
ArrayList<String[]> arrays = new ArrayList<String[]>(count);
for (int i = 0; i < count; i++)
arrays.add(bundle.getStringArray(ShowArrayActivity.ARRAY_INDEX + i));
data = arrays.toArray(new String[][]{});
}
}
#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;
}
}
In Manifest for receiver application
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.receiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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.example.receiver.ShowArrayActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.sendintent.MainActivity" />
<receiver android:name="com.example.receiver.MainActivityReceiver">
<intent-filter>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_COUNT"/>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_INDEX"/>
</intent-filter>
</receiver>
</application>
</manifest>
In receiver application, It won't show the array from sendIntent application in Interface. I just want to know the way to use broadcast and intent. So if I run in debug mode,in ShowArrayActivity, the variable data will containt the result of the intent.
So my questions is,why I can't use another class when I try to send the intent? Did I forgot something to implement for madethe communication between 2 application?
Intent intent = new Intent(this, ShowArrayActivity.class);
this way you tell activity to find ShowArrayActivity within this Application and launch it. It fails as Application can't find it in it's manifest.
Create Intent with action.
Intent intent = new Intent (NAME_OF_THE_FILTER);
then replace string it second manifest
assuming
public static final String NAME_OF_THE_FILTER = "com.myapp.myfilter"
<intent-filter>
<action android:name="com.myapp.myfilter"/>
</intent-filter>

Why isnt my phone vibrating when i press the button?

Hi im trying to make it that when a user clicks on on a button the phone vibrates. Heres manifest.xml and AndroidVibrator.java so whats wrong? how can i fix it? thanks!
heres the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".AndroidActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity2"
android:screenOrientation="portrait"
android:label="#string/app_name" >
</activity>
<activity android:name=".activity3"
android:screenOrientation="portrait"
android:label="#string/app_name" >
</activity>
<activity android:name=".next" >
</activity>
</application>
</manifest>
Heres AndroidActivity:
package android.app;
import android.app.R;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.os.Vibrator;
public class AndroidVibrator extends Activity implements OnClickListener
{
private View myView;
private Vibrator myVib;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
//myView can be any type of view, button, etc.
myView = (View) this.findViewById(R.id.sound);
myView.setOnClickListener((android.view.View.OnClickListener) this);
}
public void onClick(View v)
{
myVib.vibrate(50);
//add whatever you want after this
}
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
Make sure you call
<uses-permission android:name="android.permission.VIBRATE" />
In your manifest
EDIT:
Try putting your permission at the end of the manifest, or after version code.
I've responded to your previous post about the same matter
this time you place the permissions over uses sdk...
check my other answer.
Also check the ouput of LogCat. Does your application force closes?
may be 50 ms is too short to feel vibrate
try more than 50ms

Categories

Resources