Start activity from preference-headers

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

I'm implementing n-layer PreferenceActivities 1st layer PreferenceActivity is loaded from preference-headers.

First header creates fragment of settings which is a PreferenceFragment. Second is a browser activity (2nd is an example from developer.android.com) which opens specified Url. The third one I want to be a next level of PreferenceAtivity that also will be loaded from preference-headers.

First two work fine but 3rd is crashing an app with the exception:

"android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=com.mycompany.myapp.ui.MyPreferenceActivity} "

MyPreferenceActivity declared in the manifest file. Probably declaration of activity in main_headers.xml is wrong, but I didn't find in references any tip how to do it correct. Tried several variations, they didn't work.

Example I used: http://developer.android.com/reference/android/preference/PreferenceActivity.html

Any thoughts why it doesn't work for me or how the next PreferenceActivity can be called? Basically I just need to start an activity from header, that should be really simple but I'm missing something.

public class MySettings extends PreferenceActivity  {     @Override     public void onBuildHeaders(List<Header> target)      {         loadHeadersFromResource(R.xml.main_headers, target);     } } 

main_headers.xml:

<?xml version="1.0" encoding="utf-8"?> <preference-headers     xmlns:android="http://schemas.android.com/apk/res/android" >      <header android:title="Custom Settings"         android:fragment="com.mycompany.myapp.ui.SettingsFragment" />      <header android:title="Intent"         android:summary="Launches an Intent.">         <intent android:action="android.intent.action.VIEW"                 android:data="http://www.android.com" />     </header>      <header android:title="Another Preference Activity">         <intent android:action="android.intent.action.VIEW"             android:data="com.mycompany.myapp.ui.MyPreferenceActivity" />     </header> </preference-headers> 

回答1:

If you want to start an explicit Activity from your third preference then do this:

<intent android:targetPackage="com.mycompany.myapp"         android:targetClass="com.mycompany.myapp.ui.MyPreferenceActivity" /> 


回答2:

 <header android:title="Intent"     android:summary="Launches an Intent."> <intent android:targetPackage="com.mycompany.myapp"         android:targetClass="com.mycompany.myapp.ui.MyPreferenceActivity" /></header> 

and add the below code in mainfest file

<activity android:name=".activities.MyActivity"                android:label="@string/my_activity_title"               android:theme="@android:style/Theme.Black.NoTitleBar">         <intent-filter>            <action android:name="com.example.myapp.activities.MyActivity" />            <category android:name="android.intent.category.DEFAULT" />        </intent-filter>     </activity> 


回答3:

if you are creating preferences using java ,then you can achieve the same result by,

PreferenceScreen editPhoneNumbersScreen = getPreferenceManager().createPreferenceScreen(mContext);     editPhoneNumbersScreen.setTitle(getResources().getString(R.string.change_add_emergency_numbers));     editPhoneNumbersScreen.setSummary(getResources().getString(R.string.summary_add_no));    Intent i = new Intent(getApplicationContext(),TargetActivity.class); i.putExtra("change numbers", true);         editPhoneNumbersScreen.setIntent(i); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!