0

I am creating Preferences which has 3 options

  1. About us

  2. Contact us

  3. Help

When I run the below code on the emulator, and open up the app and click on About us , I get a message saying "Unfortunately Rost has stopped Working".

About us should display a dialog theme with the text "This ia a demo version full version will cost you around $1000"

Wondering what might be the issue.

Here is my code:

Prefs.java

import android.os.Bundle;
import android.preference.PreferenceFragment;


class Prefs extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub


        addPreferencesFromResource(R.xml.simplr_preferences);


    }

About.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This ia a demo version full version will cost you around $1000" />

</LinearLayout>

Rost manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.world.rost"
    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=".RostApp"
            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=".Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.world.rost.MENU" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:theme="@android:style/Theme.Dialog"
            android:name=".About"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.world.rost.ABOUT" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:theme="@android:style/Theme.Dialog"
            android:name=".Prefs"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.world.rost.PREFS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>




    </application>

</manifest>

simplr_preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory
        android:title="Foo" >  


    <EditTextPreference
        android:key="edit"
        android:summary="This ia edit text"
        android:title="edittext" />

    <CheckBoxPreference
         android:title="Checkbox" >
         android:key="Checkbox"
         android:defaultvalue="true"
        android:summary="This ia a check box"

    </CheckBoxPreference>

    <ListPreference
        android:title="Listpreference"
        android:key="list"
        android:summary="this is a list"
        android:entries="@array/list" />    

</PreferenceCategory>

</PreferenceScreen>

menu.java

package com.world.rost;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;




public class Menu extends ListActivity{

    String classes[] = {"MainActivity","Dummyone","Camera","Email","Data","Openedclass" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classes));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String name = classes[position];
        try {

            Class myclass = Class.forName("com.world.rost." + name);

            Intent newintent = new Intent(Menu.this,myclass);
            startActivity(newintent);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu);
        MenuInflater disp = getMenuInflater();
        disp.inflate(R.menu.simple_menu, menu);

        return true;


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch(item.getItemId())
        {

        case R.id.aboutUs:
         Intent isn = new Intent("com.world.rost.PREFS");
         startActivity(isn);

        break;

        case R.id.contactUs:

        break;


        case R.id.help:
        finish();

        break;

        }
        return false;

    }
}

logcat Error Log

09-26 09:50:20.970: D/dalvikvm(1605): GC_FOR_ALLOC freed 16K, 4% free 3364K/3476K, paused 3ms, total 4ms
09-26 09:50:20.970: I/dalvikvm-heap(1605): Grow heap (frag case) to 3.979MB for 635808-byte allocation
09-26 09:50:21.000: D/dalvikvm(1605): GC_FOR_ALLOC freed 1K, 3% free 3984K/4100K, paused 25ms, total 25ms
09-26 09:50:21.090: W/EGL_emulation(1605): eglSurfaceAttrib not implemented
09-26 09:52:26.362: W/EGL_emulation(1605): eglSurfaceAttrib not implemented
09-26 09:52:27.102: D/dalvikvm(1605): newInstance failed: Lcom/world/rost/Prefs; not accessible to Landroid/app/Instrumentation;
09-26 09:52:27.102: D/AndroidRuntime(1605): Shutting down VM
09-26 09:52:27.102: W/dalvikvm(1605): threadid=1: thread exiting with uncaught exception (group=0xb2d2db20)
09-26 09:52:27.102: E/AndroidRuntime(1605): FATAL EXCEPTION: main
09-26 09:52:27.102: E/AndroidRuntime(1605): Process: com.world.rost, PID: 1605
09-26 09:52:27.102: E/AndroidRuntime(1605): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.world.rost/com.world.rost.Prefs}: java.lang.IllegalAccessException: access to class not allowed
09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.os.Looper.loop(Looper.java:136)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at java.lang.reflect.Method.invoke(Method.java:515)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at dalvik.system.NativeStart.main(Native Method)
09-26 09:52:27.102: E/AndroidRuntime(1605): Caused by: java.lang.IllegalAccessException: access to class not allowed
09-26 09:52:27.102: E/AndroidRuntime(1605):     at java.lang.Class.newInstanceImpl(Native Method)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at java.lang.Class.newInstance(Class.java:1208)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
09-26 09:52:27.102: E/AndroidRuntime(1605):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
09-26 09:52:27.102: E/AndroidRuntime(1605):     ... 11 more
  • I have the solution, but it'll cost you around $1,000. – 323go Sep 26 '14 at 15:59
  • @323go Thanks Keep that to yourself.. Below people gave me solution for free of cost. Please don't beg in SO this is a programming community not a charity. –  Sep 26 '14 at 16:08
  • So sorry that your understanding of sarcasm is broken. Joke's on you. – 323go Sep 26 '14 at 16:38
  • @323go I know that. Bro the joke is now on you. Please read my comment. You have made a joke of yourself –  Sep 26 '14 at 16:47
  • @323go I am a beginner in Android if you are experienced that doesn't mean that you can make fun of others. Do not forget "The expert in anything was once a beginner" –  Sep 26 '14 at 16:49
  • Dude, you're asking basic questions and charging $1,000. You do realize how silly that is? – 323go Sep 26 '14 at 17:04
  • I quoted you on the $1,000. Your comment would then apply to you, no? – 323go Oct 27 '14 at 12:59

2 Answers2

0

Check your preferences XML file again! There is some XML syntax errors!

<CheckBoxPreference
         android:title="Checkbox" 
         android:key="Checkbox"
         android:defaultvalue="true"
        android:summary="This ia a check box" >

</CheckBoxPreference>

After that, you can't start an Activity when the file you are pointing at is not an Activity!

Your preferences file is a PreferenceFragment which need to be hosted inside a FragmentActivity or an Activity containing this Fragment.

Make sure you go over the official documentation once again http://developer.android.com/guide/topics/ui/settings.html

Pavlos
  • 2,183
  • 2
  • 20
  • 27
0

This is your main error. You cannot start a Fragment with an intent:

Intent isn = new Intent("com.world.rost.PREFS");

Intent can be used to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

You need to host that Fragment inside a FragmentActivity or a related class.

"PREFS" is not an Activity inside the package "com.world.rost"

It is a Fragment

A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle.

Here is a related question that can help you

Also, as @Pavlos mentioned, you have some syntactic XML errors as well.

Community
  • 1
  • 1
nem035
  • 34,790
  • 6
  • 87
  • 99
  • Ok Thanks for this. So after I embed the code inside the PreferenceActivity class. Are there any mandatory methods that I should include in the class ? –  Sep 26 '14 at 14:33
  • 1
    Instead of me writing stuff in a comment with limited space, Google already tells you pretty much everything you need to know http://developer.android.com/reference/android/preference/PreferenceActivity.html – nem035 Sep 26 '14 at 14:35