I am a newbie in android, I am creating a test program to test the search dialog of android.
this is searchable.xml config file
<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="Search"
android:hint="Type text to search">
</searchable>
this is manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="luan.com.bk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity android:name=".SearchActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name=".TestActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
</application>
</manifest>
this is the searchActivity.java to request search
package luan.com.bk;
import android.app.Activity;
import android.os.Bundle;
public class SearchActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
this is the testActivity.java init the search dialog
package luan.com.bk;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
But when I press the search key button on android simulator, the search bar not display, what is I wrong? please help. sorry because my english is not good.
Related
I am trying to start a New Activity using Intent but I keep getting java.lang.IllegalStateException: Could not execute method of the activity
What am I doing wrong?
Main Activity
package com.example.wrw;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clicked(View v)
{
Intent intent = new Intent(this, newact.class);
startActivity(intent);
}
}
newact.java
package com.example.wrw;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class newact extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newlayout);
}
}
act_main.xml
<LinearLayout 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"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Act"
android:onClick="clicked"
/>
</LinearLayout>
newlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wrw"
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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In android app, all Activity must be configured in the Manifest.xml file firstly, so you should add newact Activity to your manifest
<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="com.example.wrw.newact"></activity>
Add this to your manifest file
<activity
android:name=".newact"
android:label="#string/title_activity_login">
Replace this to your MainActivity, this may resolve your problem.
Intent intent = new Intent(MainActivity.this, newact.class);
startActivity(intent);
After this, do not forget to add this in your android menifest file,
<activity
android:name=".newact"
android:label="#string/app_name">
</activity>
try this
Intent intent = new Intent(MainActivity.this, newact.class)
and also register newact in manifest file like this
<activity
android:name=".newact"
android:label="#string/app_name">
</activity>
I am working on a basic fragment application from the tutorials. All I am doing is adding fragment layout and inflating it. I have attached a class with the fragment as mentioned in the tutorials, but when I run the application, it fails with message "Unfortunately, 'App' has stopped".
Any help would be appreciated.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fragments"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ArticleFragment"></activity>
</application>
</manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.fragments.ArticleFragment"
android:id="#+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.Java
package com.example.fragments;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ArticleFragment.Java
package com.example.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ArticleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_main, container, false);
}
}
In ArticleFragment in onCreateView you're inflating R.layout.activity_main. That's propably the problem. And delete this entry from Manifest <activity android:name=".ArticleFragment"></activity>.
Change your ArticleFragment activity's manifest entry to this:
<activity
android:name=".ArticleFragment"
android:label="Article Fragment" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I try to write app which can start when usb connect
I learn from Starting my android application automatically after connecting the USB cable.
my code is
package com.example.formatsdcard;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MainActivity extends Activity {
public class OnPowerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton01 = (Button)findViewById(R.id.button1);
mButton01.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
wipeMemoryCard();
}
});
MY Manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.formatsdcard"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.formatsdcard.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=".OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
</application>
my app can run after I click it but it will show there're some problem try again later when I connect my usb cable.
How should I change my code to let it work normally.
You should probably try
<receiver android:name="MainActivity$OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
Or try putting OnPowerReceiver as a public class in your package com.example.formatsdcard
Create a own class for your onPowerReceiver. Don't put it in your MainActivity.
I'm very new in android and i am working on a app.
I meet problem in linking the 2nd page to the 3rd page while clicking the button. I had tried to solve the problem but it do not work. Below is my AndroidManifest.xml
`
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fyp"
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=".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>
<activity
android:name=".BelajarActivity"
android:label="#string/title_activity_main">
</activity>
<activity
android:name=".KnamaActivity"
android:label="#string/title_activity_main">
</activity>
</application>
</manifest>
below is BelajarActivity.java
package com.example.fyp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class BelajarActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.belajar);
Button bnama = (Button) findViewById(R.id.knama);
bnama.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent namaIntent = new Intent(BelajarActivity.this,KnamaActivity.class);
startActivity(namaIntent);
}
});
}
}
This
bnama.setOnClickListener(new Button.OnClickListener() { //this is wrong
...
});
should be
bnama.setOnClickListener(new View.OnClickListener() {
..
});
OnClickListener should be of type View
Just change this line
bnama.setOnClickListener(new View.OnClickListener() {
I am using the Device Policy Manager to lock the android phone immediately and here is my activity code:
package com.husam.admin;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
public class AdminActivity extends Activity {
/** Called when the activity is first created. */
protected static final int REQUEST_CODE_ENABLE_ADMIN=1;
DevicePolicyManager dpm;
ComponentName mAdminName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dpm=(DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mAdminName=new ComponentName(this,MyAdmin.class);
Button button=(Button)findViewById(R.id.admin);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(dpm.isAdminActive(mAdminName))
{
Log.w("Yes admin","Locking Now");
dpm.lockNow();
}
else
{Intent intent1 =new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent1.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent1.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Need new Admin");
Log.w("no Admin","Set admin");
startActivityForResult(intent1,REQUEST_CODE_ENABLE_ADMIN);
}
}
});
}
class MyAdmin extends DeviceAdminReceiver{
void OnEnabled(){
}
void onDisable(){
}
}
}
and My Mainfest.xml fill is as follow:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.husam.admin"
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=".AdminActivity"
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=".MyAdmin"
android:label="#string/device_admin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="#xml/my_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>
</manifest>
and my_admin.xml which is inside xml folder under res folder is as follows:
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>
and when I run my app I faced an error in my Log file state that:
W/DeviceAdminAdd(433): Unable to retrieve device policy ComponentInfo{com.husam.admin/com.husam.admin.AdminActivity$MyAdmin}
I searched this website for similar issued but all what I found is that the error was in the mainfest file in closing the receiver before including the meat and intent filter inside it which is not as my case.
So any help or redirection to solve this issue will be completely appreciated
regards
Husam
I think I figured out the answer to the question I posted above. What I need to do is that in registering my receiver in my mainfest file I have to do the following:
<receiver
android:name=".AdminActivity$MyAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="#xml/my_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
Note:
in android:name I have to write .AdminActivity$Myadmin instead of .Myadmin, since my class for administration broadcast receiver is an inner class in my activity, not a separate one.