Failed to start my preference activity - android

Currently I am trying to add a preference activity into my application
but found out that I couldn't make it works.
Every time, I tried to start the preference activity but it just crash before showing anything.
Here is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="keysquare.android"
android:versionCode="1"
android:versionName="0.9">
<application android:icon="#drawable/icon" android:label="#string/ime_name" android:debuggable="true">
<service android:name="KeysquareAndroid" android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data android:name="android.view.im" android:resource="#xml/method" />
</service>
<activity android:label="KeysquareAndroidSettings" android:name="KeysquareAndroidSettings" android:exported="true" android:enabled="true"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
And the preference xml, which I have tried my best to trim it down.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>
And finally the preference activity class, which also looks normal to me...
package keysquare.android;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class KeysquareAndroidSettings extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Thanks in advance if anyone can help.
Jeanno

Thanks Mathias.
Finally the problem was solved.
It seems that I shouldn't call activities from a service.
Also, in the method.xml, I should reference to a full package instead of using a
local reference.
Jeanno

Related

Extended Application can not run correctly

everyone. I am new to android.
Recently, I tried to learn how to extend Application, but there is
a strange problem. I have tried a lot of methods, but still can not
solve it. So, thanks in advance if anyone can help me.
The code section is like this:
package com.example.xw.myfirstapplication;
import android.app.Application;
/**
* Created by Administrator on 2016/8/26.
*/
public class MyApplication extends Application {
public static MyApplication mApplication;
#Override
public void onCreate(){
super.onCreate();
mApplication = this;
}
public static MyApplication getMyApplication(){
return mApplication;
}
}
And, Manifest.xml is like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xw.myfirstapplication"
>
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="24" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:name="com.example.xw.myfirstapplication.MyApplication"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
>
<!--<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>-->
<!--<uses-permission android:name="android.permission.INTERNET"></uses-permission>-->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--<activity android:name="testNewTech.DisplayMessageActivity" />
<activity android:name="testNewTech.NotificationActivity"></activity>-->
</application>
</manifest>
But, when I run the code, something happens very quickly, like this:
In addition:
1. I tried not to use extended Application but default Application, everything is good.
I add Log.v("MainActivity", "start onCreate"); in method onCreate() of class MainActivity. But, there is nothing in logcat, so I think the method onCreate() of class MainActivity is not processed.
So, anyone has any advice? Thank you so much!
If you can see in your manifest.xml file then you can pass another class
<application android:name="com.example.xw.myfirstapplication.MyApplication"
...
...
>
just change
<application android:name="com.example.xw.myfirstapplication.Application"
...
...
>

ActivityNotFoundException when trying to invoke an 'explicit' intent from Preferences, to open an Activity in default package?

In the following SSCCE I am trying to invoke an explicit intent from preferences.xml, to open an Activity which is located in the one and only package in the app, in which all Activity's are located.
But I get the following exception:
android.content.ActivityNotFoundException: No Activity found to handle Intent { }
I have seen this question but it is about starting an Activity in another package, and somebody said in that question that their app to open Activity in default package works fine.
Following are the relevant parts of code.
NOTE: Since SecondActivity is in the same package as the MainActivity, I initially tried to use only one android:targetClass attribute to the <intent> in the preferences.xml, but then after the exception I added the android:targetPackage too, but that did not solve the problem.
MainActivty.java:
package practice.preferences_practice;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class MainActivity extends PreferenceActivity {
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<Preference android:key="#+id/preferences_preference_preferenceContainingIntent"
android:title="#string/preferences_preference_preferenceContainingIntent_title"
android:summary="#string/preferences_preference_preferenceContainingIntent_summary" >
<intent android:targetPackage="practice.preferences_practice"
android:targetClass="practice.preferences_practice.SecondActivity" />
</Preference>
</PreferenceScreen>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="practice.preferences_practice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<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=".SecondActivity"
android:label="#string/title_activity_second" >
</activity>
</application>
</manifest>
NOTE: I have Not used an <intent-filter> in Manifest for the SecondActivity because it lies in the same default package as the MainActivity, which is practice.preferences_practice.
NOTE: If you think I should post all the other code files as well, please let me know.
EDIT:
res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Preferences Practice</string>
<string name="hello_world">Hello world!</string>
<string name="preferences_preference_preferenceContainingIntent_title">Preferece Title</string>
<string name="preferences_preference_preferenceContainingIntent_summary">Opens another activity because this preference contains and invokes an Intent.</string>
<string name="title_activity_second">SecondActivity</string>
</resources>
res/layout/activity_second.xml
<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"
android:background="#F4A460"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
SecondActivity.java:
package practice.preferences_practice;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
You code is as it should be. It's working pretty fine as I also tested it. Try to clean build the project and reinstall the apk.
Use startActivity(new Intent(MainActivity.this, SecondActivity.class)); to start second activity.

Layout does not appear in android emulator

I'm new to Android programming and i'm doing tutorial from layouts from http://developer.android.com/resources/tutorials/views/index.html . Relative layout tutorial exactly. I did everything they say but when I try to start app in android emulator there are no buttons, textfields etc. Only name of app shows at the top. What is wrong? Is this a problem with emulator? I'm using Eclipse Version: 3.7.1.
EDIT:
package pchot.tutorial;
import android.app.Activity;
import android.os.Bundle;
public class Tutorial1Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pchot.tutorial"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Tutorial1Activity"
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>
Layout is copy/paste from http://developer.android.com/resources/tutorials/views/hello-relativelayout.html
EDIT:
Ok, problem solved. I needed to delete "Hello World, Tutorial1Activity!" from string.xml file which was autocreated during creation of project in Eclipse.
Have you modified you manifest.xml file? Maybe you haven't modify it to make it aware that the launched main activity is the activity you've just created.
You didn't declare anything in your onCreate() method.
For Button declare Button btn = (Button)findViewById(R.id.btnSubmit)
Implement onClickListener if you want the button to listen to clicks.
For TextView, declare TextView tv = (TextView)findViewById(R.id.textview)

Getting Blank Screen when Calling StartActivity()

Thanks in advance for your efforts.
I have an Application that revolves around a ListActivity extended class. Before the app starts, I want to check whether the user is registered, and if not tell him to and get some info from him. So, I tried to call StartActivity in the OnCreate() method. When that loaded, I got a big black screen.
I thought that it may be related to being run in the OnCreate so I let the Activity start as usual, and I tried to run it in an OnClick event, and I got the same result.
In both cases, if I press escape, that Window goes away and the main app window comes back.
Here are the lines from where I call the new Activity
Intent emailIntent = new Intent(this, EmailAddressGetter.class);
this.startActivity(emailIntent);
Here is the code of the class
/**
*
*/
package com.kosherapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
/**
* #author Josh
*
*/
public class EmailAddressGetter extends Activity {
public void OnCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emailinput);
}
}
Here is the emailinput xml contents
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:text="hello"
>
</TextView>
And, here is the manifest contents
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kosherapp"
android:versionCode="1"
android:versionName="1.0"
>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
>
<activity
android:name=".KosherApp"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
>
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EmailAddressGetter"
android:label="email Address Getter"
>
</activity>
<activity
android:name=".GoogleMaps"
android:label="Google Maps"
>
</activity>
<uses-library
android:name="com.google.android.maps" />
</application>
</manifest>
Let me know if there's any other info you may need. Oh, I'm running this with the Google API 2.1-update
Thanks
Change OnCreate to onCreate in EmailAddressGetter.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emailinput);
}
You need to use EditText instead. TextView is for static text which is why nothing is displaying. You should also include the EditText in another layout like Linear or Relative so it looks cleaner.
You are using an intent to call EmailAddressGetter but it doesn't have an intent filter in the manifest. Throw in something like Log.e("EmailAddressGetter", "It worked") in your onCreate and use ddms to see if it is actually opening that activity.
Also, in the updated code in your comment to Robby Pond, remove the xmlns line from EditText

Any fixes for crash because of combination of WRITE_SETTINGS and SEARCH_LONG_PRESS?

I'm trying to create an app that will change a setting when the user holds the search button. It seems that the combination of the WRITE_SETTINGS permission and the fact that the activity is launched from another application using the long press makes the parent task crash. For example, if I'm looking at Google Maps and hold the search button, the SearchButtonPopup does not appear and Google Maps crashes.
I tried taking the Theme.Dialog out, but it didn't make any difference. Removing the permission makes it robust again, no crashes, but I need the permission! Any fixes or workarounds?
These are my manifest and java files:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.stathissideris.searchbuttonpopup" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application android:icon="#drawable/logo" android:label="#string/app_name"
android:debuggable="true" android:permission="android.permission.WRITE_SETTINGS">
<activity android:name=".SearchButton" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.SEARCH_LONG_PRESS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
SearchButton.java
package org.stathissideris.searchbuttonpopup;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class SearchButton extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("SearchButtonPopup", "search held");
}
}

Categories

Resources