i have a application that allows me to alter/add/delete from mysql database. The application opens up fine when i run it but when i try clicking on the 'ViewAllEmployee' button i keep getting a error saying it cannot find explicit activity class. I have looked online and tried all solutions that have worked for people with this problem before. I have added it into my AndroidManifest too but it still doesn't want to work. Can anyone help? Literally been trying to fix this for hours!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment.androidassignment"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:label="#string/app_name" >
<activity
android:name="com.example.assignment.androidassignment.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name="com.example.assignment.androidassignment.ViewAllEmployee"
/>
</activity>
</application>
</manifest>
MainActivity.java -- Error showing when onClick for 'ViewAllEmployee' is clicked:
package com.example.assignment.androidassignment;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.HashMap;
public class MainActivity extends Activity implements View.OnClickListener{
//Defining views
private EditText editTextLatitude;
private EditText editTextLongitude;
private EditText editTextTimeInserted;
private Button buttonAdd;
private Button buttonView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing views
editTextLatitude = (EditText) findViewById(R.id.editTextLat);
editTextLongitude = (EditText) findViewById(R.id.editTextLon);
editTextTimeInserted = (EditText) findViewById(R.id.editTextTimeInserted);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonView = (Button) findViewById(R.id.buttonView);
//Setting listeners to button
buttonAdd.setOnClickListener(this);
buttonView.setOnClickListener(this);
}
//Adding an employee
private void addEmployee(){
final String lat = editTextLatitude.getText().toString().trim();
final String lon = editTextLongitude.getText().toString().trim();
final String timeInserted = editTextTimeInserted.getText().toString().trim();
class AddEmployee extends AsyncTask<Void,Void,String>{
ProgressDialog loading;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this,"Adding...","Wait...",false,false);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
}
#Override
protected String doInBackground(Void... v) {
HashMap<String,String> params = new HashMap<>();
params.put(Config.KEY_LAT,lat);
params.put(Config.KEY_LON,lon);
params.put(Config.KEY_TIMEINSERTED,timeInserted);
RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(Config.URL_ADD, params);
return res;
}
}
AddEmployee ae = new AddEmployee();
ae.execute();
}
#Override
public void onClick(View v) {
if(v == buttonAdd){
addEmployee();
}
if(v == buttonView){
startActivity(new Intent(this,com.example.assignment.androidassignment.ViewAllEmployee.class));
}
}
}
LogCat errors:
12-09 03:34:46.634 2489-2489/? E/AndroidRuntime: FATAL EXCEPTION: main
=Process: com.example.assignment.androidassignment, PID: 2489
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.assignment.androidassignment/com.example.assignment.androidassignment.ViewAllEmployee}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
at android.app.Activity.startActivityForResult(Activity.java:3424)
at android.app.Activity.startActivityForResult(Activity.java:3385)
at android.app.Activity.startActivity(Activity.java:3627)
at android.app.Activity.startActivity(Activity.java:3595)
at com.example.assignment.androidassignment.MainActivity.onClick(MainActivity.java:91)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
You are nesting <activity> tags in your Manifest. Take them apart:
<activity
android:name="com.example.assignment.androidassignment.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.assignment.androidassignment.ViewAllEmployee"/>
from
if(v == buttonView){
startActivity(new Intent(this,com.example.assignment.androidassignment.ViewAllEmployee.class));
}
to
if(v == buttonView){
startActivity(new Intent(MainActivity.this,ViewAllEmployee.class));
}
from
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment.androidassignment"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:label="#string/app_name" >
<activity
android:name="com.example.assignment.androidassignment.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name="com.example.assignment.androidassignment.ViewAllEmployee"
/>
</activity>
</application>
</manifest>
to
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment.androidassignment"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:label="#string/app_name" >
<activity
android:name="com.example.assignment.androidassignment.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.assignment.androidassignment.ViewAllEmployee"
/>
</application>
</manifest>
please change the above and this is happening because of nesting of activity inside manifest
Related
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>
I'm getting a nullPointerException when I try to access my ActionBar
getActionBar().setDisplayHomeAsUpEnabled(true);
my recipientsActivity is this
package com.ahmetyuva.ribbit;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import java.util.List;
public class RecipientsActivity extends ListActivity{
public static final String TAG = RecipientsActivity.class.getSimpleName();
protected List<ParseUser> mFriends;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected MenuItem mSendMenuItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipients);
getActionBar().setDisplayHomeAsUpEnabled(true);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
#Override
public void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
query.addAscendingOrder(ParseConstants.KEY_USERNAME);
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> friends, ParseException e) {
if(e == null) {
mFriends = friends;
String[] usernames = new String[mFriends.size()];
int i = 0;
for (ParseUser user : mFriends) {
usernames[i] = user.getUsername();
i++;
} ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getListView().getContext(),
android.R.layout.simple_list_item_checked,
usernames);
setListAdapter(adapter);
}
else{
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recipients, menu);
mSendMenuItem = menu.getItem(0);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_send:
return true;
}
/* int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
*/
return super.onOptionsItemSelected(item);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (l.getCheckedItemCount() > 0) {
mSendMenuItem.setVisible(true);
} else {
mSendMenuItem.setVisible(false);
}
}
}
my error log is here
04-13 11:19:05.148 1956-1956/? E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ahmetyuva.ribbit/com.ahmetyuva.ribbit.RecipientsActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.ahmetyuva.ribbit.RecipientsActivity.onCreate(RecipientsActivity.java:40) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)
menu_recipients xml is here
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.ahmetyuva.ribbit.RecipientsActivity">
<item android:id="#+id/action_send"
android:title="Send"
android:orderInCategory="100"
app:showAsAction="always"
android:visible="true"
android:icon="#drawable/ic_action_send_now"
/>
</menu>
activity_recipients xml is here
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ahmetyuva.ribbit.RecipientsActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#android:id/list"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"/>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/empty_recipients_list_message"
/>
</RelativeLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ahmetyuva.ribbit" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission
android:name="com.ahmetyuva.ribbit.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.ahmetyuva.ribbit.permission.C2D_MESSAGE" />
<application
android:name=".RibbitApplication"
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=".LoginActivity"
android:label="#string/title_activity_login"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".SignUpActivity"
android:label="#string/title_activity_sign_up"
android:parentActivityName=".LoginActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".EditFriendsActivity"
android:label="#string/title_activity_edit_friends"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.ahmetyuva.ribbit.MainActivity" />
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
<category android:name="com.ahmetyuva.ribbit" />
</intent-filter>
</receiver>
<activity
android:name=".RecipientsActivity"
android:label="#string/title_activity_recipients"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.ahmetyuva.ribbit.MainActivity" />
</activity>
</application>
</manifest>
Try to use
getSupportedActionBar() instead of getActionBar()
First of all, always do this to avoid this kind of error :
ActionBar ab = getSupportActionBar();
if (ab == null) {
Log.d("test", "ab null");
}
else {
Log.d("test", "ab not null");
ab.setDisplayHomeAsUpEnabled(true);
}
Then, what is in the logcat with the tag "test" ?
Make sure that you actually have an ActionBar, if for example, your theme is:
Theme.Light.NoActionBar, you won't have an action bar.
So, make sure your theme has and Actionbar
EDIT:
Try with the following theme:
<style name="AppTheme" parent="Theme.AppCompat">
If that doesn't work, You should consider changing to fragments, as suggested in this SO Answer
For both activities,
in your onCreate the next line after setContentView( ... )
add the following :
getActionBar().setDisplayHomeAsUpEnabled(true);
in the manifest for both activities add
android:theme="#android:style/Theme.Holo.Light"
in the tag directly after
android:screenOrientation="portrait"
I want to do a simple exercise with permissions. I need to define and enforce a custom permission for my app called "DangerousApp" And after that I need to set up another app, "PermissionsLab" in such a way that it could use the "DangerousApp" but when I try to start the "DangerousApp" using "PermissionLab" I get an error: "Unfortunately, PermissionsLab has stopped"
AndroidManifest.xml of PermissionsLab:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.labs.permissionslab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- TODO - add uses-permission elements -->
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ActivityLoaderActivity"
android:label="#string/title_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BookmarksActivity"
android:label="#string/title_permissions" >
</activity>
<activity
android:name=".GoToDangerousActivity"
android:label="#string/title_activity_customization" >
</activity>
</application>
</manifest>
AndroidManifest.xml of DangerousApp:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.labs.permissionslab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- TODO - add uses-permission elements -->
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ActivityLoaderActivity"
android:label="#string/title_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BookmarksActivity"
android:label="#string/title_permissions" >
</activity>
<activity
android:name=".GoToDangerousActivity"
android:label="#string/title_activity_customization" >
</activity>
</application>
</manifest>
And the activity used to start DangerousApp:
package course.labs.permissionslab;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GoToDangerousActivity extends Activity {
private static final String TAG = "Lab-Permissions";
private static final String DANGEROUS_ACTIVITY_ACTION = "course.labs.permissions.DANGEROUS_ACTIVITY";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.go_to_dangerous_activity);
Button startDangerousActivityButton = (Button) findViewById(R.id.start_dangerous_activity_button);
startDangerousActivityButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startDangerousActivity();
}
});
}
private void startDangerousActivity() {
Log.i(TAG, "Entered startDangerousActivity()");
startActivity(new Intent(DANGEROUS_ACTIVITY_ACTION));
}
}
Thanks for your clarifications.
Permission help you to protect service, application, activity, ...
Here documentation:
http://developer.android.com/guide/topics/security/permissions.html#declaring
http://developer.android.com/guide/topics/security/permissions.html#manifest
for instance, if you want to protect an activity with a permission, in AndroidManifest declare a permission and put it on activity as this:
<manifest [...] >
<permission android:name="com.mycompany.MY_PERMISSION" android:protectionLevel="normal"
android:description="#string/permission_desc"
android:label="#string/permission_label" />
<application [...] >
<activity [...] android:permission="com.mycompany.MY_PERMISSION">
</activity>
</application>
</manifest>
In another application, if you want to call activity protected by permission, you have to add in AndroidManifest:
<uses-permission android:name="com.mycompany.MY_PERMISSION" />
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
I'm trying to let a user press a button so it would vibrate. So i declared it in manifest and i made a new activity for it. Heres some code did i misplace the permissions?
<?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-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>
<uses-permission android:name="android.permission.VIBRATE"/>
</application>
</manifest>
and heres the activity for the button:
package android.app;
import android.app.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AndroidVibrator extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main3);
final Button button = (Button) findViewById(R.id.sound);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
Try the following -:
?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-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application>
.
.
.
</application
First of all if you are developing in Eclipse use, ctrl+A and then ctrl+I this should correctly manage indentation for you. Returning to your question I hope you do replace names of activity1 and activity2 etc. with what you really have like "AndroidVibrator". And lastly it should make no difference where you place the permissions although it is a better practice to have them at the top , between uses-sdk and application.
Also I see you are using android:screenOrientation="portrait", if you want your activity not to react to screen orientation changes then add that too: android:configChanges="orientation|keyboardHidden|keyboard"
And add that to your activity class:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}