Start new Activity from menu - java

I wrote simple android app with bugs. When open second activity from menu- android stopped apps.
Code:
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Activit"
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=".Activit2"/>
</manifest>
First Activity:
package com.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class Activit extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.me, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.ok: Activ();
};
return super.onOptionsItemSelected(item);
}
private void Activ(){
Intent inten=new Intent();
inten.setClass(Activit.this, Activit2.class);
startActivity(inten);
}
}
Second Activity with temporary layout(it the same in the first):
package com.example;
import android.app.Activity;
import android.os.Bundle;
public class Activit2 extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

your tag <Activity /> should be in ( not out ) of the tag <application> :
try this :
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Activit"
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=".Activit2"/> // you should declare your activity Here
</application>

The tag <activity android:name=".Activit2"/> should be inside the <application> tag in your Manifest.xml

Your second activity definition is outside the application definition in your AndroidManifest.xml.

Related

opening video in new activity

I am trying to change the main activity in android to another activity and playing a video in the new activity. but, even I changed in the manifest the main activity, it didn't show me the video when I'm starting the app. I tried to Check in the Logcat if the activity is still launched, and i didn't see any message.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mta2223">
<uses-permission android:name="android.permission.INTERNET" />
<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/Theme.MTA2223">
<activity
android:name=".Entering"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityShowingPlayers"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
</application>
</manifest>
The new activity
package com.example.mta2223;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
public class Entering extends AppCompatActivity {
VideoView videoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entering);
videoView= (VideoView) findViewById(R.id.videoV);
videoView.setVideoPath("res/raw/intrologo.3gp");
videoView.start();
while (videoView.isPlaying()){
Log.d("HEy","hey");
}
videoView.stopPlayback();
Intent i = new Intent(this,MainActivity.class);
startActivity(i);
finish();
}
}
Try this
In this code get video path from raw folder
String path = "android.resource://com.example.mta2223" + "/" + R.raw.intrologo;
videoView.setVideoPath(path);
videoView.start();

onServiceConnected not called after enable my AccessibilityService

After I open my app, it jumps to the accessibility settings. But the onServiceConnected is not called after I have turned on my AccessibilityService (which is called VoiceService).
Could you please tell me what should I do? It seems that the VoiceService fails to start although I enable it in XML. Or I need to bind the MainActivity with the VoiceService in a different way?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wang.hearexpr">
<uses-permission
android:enabled="true"
android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
<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>
<service android:name=".VoiceService"
android:enabled="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="#string/service_name">
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/voice_config"/>
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"/>
</intent-filter>
</service>
</application>
</manifest>
voice_config.xml
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:description="#string/service_description"
android:packageNames="com.example.wang.hearexpr"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"/>
VoiceService.java
package com.example.wang.hearexpr;
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
public class VoiceService extends AccessibilityService {
private static final String TAG = "hear the voice";
#Override
public void onServiceConnected(){
super.onServiceConnected();
Log.d(TAG, "Service Connected");
}
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d(TAG,"onAccessibilityEvent: "+ event.toString());
}
#Override
public void onInterrupt() {
Log.d(TAG, "Interrupted");
}
#Override
public void onDestroy(){
Log.d(TAG, "Destroyed");
}
}
MainActivity.java
package com.example.wang.hearexpr;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private static final Intent sSettingsIntent =
new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivityForResult(sSettingsIntent,0);
}
}
This is the answer:
in the voice_config.xml
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity" should be modified as your own activity name.

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>

Receiving Android SMS

I have been trying so many different things to get this demo running but i couldn't manage to make it run properly..
This is my Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.messagekeeper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="9" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.messagekeeper.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>
<receiver android:name=".SmsReciever">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
This is my MainActivity class (Done nothing with it):
package com.example.messagekeeper;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#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.activity_main, menu);
return true;
}
}
And This is the SmsReciever class:
package com.example.messagekeeper;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;
class SmsReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
Object[] pdus=(Object[])intent.getExtras().get("pdus");
SmsMessage shortMessage=SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.d("SMSReceiver","SMS message sender: "+
shortMessage.getOriginatingAddress());
Log.d("SMSReceiver","SMS message text: "+
shortMessage.getDisplayMessageBody());
}
}
When i get a message, i want it to see it on my app. In this situation, i mean when i use this codes and run my app on emulator, and send a message from one emulator to another, my app crashes and i get this error: http://puu.sh/1yZub
If you've copied your code exactly as it stands, you might be just missing the public keyword at the beginning.
Change class SmsReciever extends BroadcastReceiver{
to public class SmsReciever extends BroadcastReceiver{

Categories

Resources