Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What i am doing:: I am trying to use Myapplication class to send the data to next activity

Problem i am facing:: Having class cast exception

BLD_IndividualListOfItems_Starters.java

public class BLD_IndividualListOfItems_Starters extends Activity{
    // Declare Variables
        JSONObject jsonobject;
        JSONArray jsonarray;
        ListView listview;
        ListViewAdapterForAtomicListItemtype adapter;
        ProgressDialog mProgressDialog;
        ArrayList<HashMap<String, String>> arraylist;
        static String NAME = "rank";
        Button btn;

        String TYPE_FILTER;

        StringBuilder result;


        MyApplication mApplication;

        @Override
        public void onCreate(Bundle savedInstanceState) {



            super.onCreate(savedInstanceState);

            mApplication = (MyApplication)getApplication();
            // Get the view from listview_main.xml
            setContentView(R.layout.listview_main);

            TYPE_FILTER = getIntent().getExtras().getString("key_title");
            Log.v("---- Value-Start---", TYPE_FILTER);
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);

            btn = (Button) findViewById(R.id.button1);
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    result = new StringBuilder();
                    for (int i = 0; i < arraylist.size(); i++) {
                        if (adapter.mysparse.get(i) == true) {

                            result.append(arraylist.get(i).get(BLD_IndividualListOfItems_Starters.NAME));
                            result.append("
");
                        }

                    }
                    Intent n = new Intent(BLD_IndividualListOfItems_Starters.this, ResultActivity.class);
                    n.putExtra("buffer", result.toString());
                    startActivity(n);
                }
            });

            // Execute DownloadJSON AsyncTask
            new DownloadJSON().execute();
        }

        // DownloadJSON AsyncTask
        private class DownloadJSON extends AsyncTask<Void, Void, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Create a progressdialog
                mProgressDialog = new ProgressDialog(BLD_IndividualListOfItems_Starters.this);
                // Set progressdialog title
                //mProgressDialog.setTitle("Fetching the information");
                // Set progressdialog message
                mProgressDialog.setMessage("Loading...");
                mProgressDialog.setIndeterminate(false);
                // Show progressdialog
                mProgressDialog.show();
            }

            @Override
            protected Void doInBackground(Void... params) {
                // Create an array
                arraylist = new ArrayList<HashMap<String, String>>();

                String newurl = "?" + "Key=" + TYPE_FILTER;


                // Retrieve JSON Objects from the given URL address
                jsonobject = JSONfunctions.getJSONfromURL("http://54.218.73.244:7005/RestaurantAtomicListItemType/"+newurl);

                try {
                    // Locate the array name in JSON
                    jsonarray = jsonobject.getJSONArray("restaurants");

                    for (int i = 0; i < jsonarray.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        jsonobject = jsonarray.getJSONObject(i);
                        // Retrive JSON Objects
                        map.put(BLD_IndividualListOfItems_Starters.NAME, jsonobject.getString("MasterListMenuName"));

                        // Set the JSON Objects into the array
                        arraylist.add(map);
                    }
                } catch (JSONException e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void args) {
                // Pass the results into ListViewAdapter.java
                adapter = new ListViewAdapterForAtomicListItemtype(BLD_IndividualListOfItems_Starters.this, arraylist);
                // Set the adapter to the ListView

                mApplication.setArrayListMapData(arraylist);

                listview.setAdapter(adapter);
                // Close the progressdialog
                mProgressDialog.dismiss();
            }
        }
    }

ResultActivity.java

public class ResultActivity extends ListActivity {

    ListView lstView; 
    ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>(); 
    String myName;
    MyApplication mApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        for(int i=0;i<10;i++)
        {
            HashMap<String,String> map = new HashMap<String,String>();
            map.put("key", "value"+i);
            arraylist.add(map);

        }

        String[] from = { "key" };
        int[] to = { R.id.textView1 };
        SimpleAdapter adapter= new SimpleAdapter(this, arraylist,R.layout.custom_single_list, from, to);
        setListAdapter(adapter);

    }

}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.multitabcheckboxselection"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.multitabcheckboxselection.BreakfastLunchDinnerIndividualListOfItems"
            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.multitabcheckboxselection.BLD_IndividualListOfItems_Starters" />
        <activity android:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_MainCourse" />
        <activity android:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_SideCourse" />
        <activity android:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Others" />
        <activity android:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Desert" />
        <activity android:name="com.example.multitabcheckboxselection.ResultActivity" />
    </application>
    <application
        android:name="com.android.app.MyApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
    </application>

</manifest>

ListViewAdapterForAtomicListItemtype.java

public class ListViewAdapterForAtomicListItemtype extends BaseAdapter implements OnCheckedChangeListener {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();
    SparseBooleanArray mysparse;

    public ListViewAdapterForAtomicListItemtype(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        mysparse = new SparseBooleanArray(data.size());
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView name;
        CheckBox chk;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item_for_atomic_list_item_type, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        name = (TextView) itemView.findViewById(R.id.textView_id_atomic_list_item_type);
        chk = (CheckBox) itemView.findViewById(R.id.checkBox_atomic_list_item_type_id);


        // Capture position and set results to the TextViews
        name.setText(resultp.get(BLD_IndividualListOfItems_Starters.NAME));

        chk.setTag(position);
        chk.setChecked(mysparse.get(position, false));
        chk.setOnCheckedChangeListener(this);

        return itemView;
    }

    public boolean isChecked(int position) {
        return mysparse.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mysparse.put(position, isChecked);

    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        mysparse.put((Integer) buttonView.getTag(), isChecked);
    }
}

Log::

01-03 13:30:12.828: D/AndroidRuntime(461): Shutting down VM
01-03 13:30:12.828: W/dalvikvm(461): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-03 13:30:12.868: E/AndroidRuntime(461): FATAL EXCEPTION: main
01-03 13:30:12.868: E/AndroidRuntime(461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.multitabcheckboxselection/com.example.multitabcheckboxselection.BreakfastLunchDinnerIndividualListOfItems}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.multitabcheckboxselection/com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Starters}: java.lang.ClassCastException: android.app.Application
01-03 13:30:12.868: E/AndroidRuntime(461):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-03 13:30:12.868: E/AndroidRuntime(461):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-03 13:30:12.868: E/AndroidRuntime(461):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-03 13:30:12.868: E/AndroidRuntime(461):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-03 13:30:12.868: E/AndroidRuntime(461):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 13:30:12.868: E/AndroidRuntime(461):  at android.os.Looper.loop(Looper.java:123)
01-03 13:30:12.868: E/AndroidRuntime(461):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-03 13:30:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
178 views
Welcome To Ask or Share your Answers For Others

1 Answer

Move the attribute

android:name="com.android.app.MyApplication"

to the first application element in the manifest and delete the second application element.

Btw. is your MyApplication class really in the package com.android.app?

EDIT: you use a different package, so the line should be

android:name="com.example.multitabcheckboxselection.MyApplication"

EDIT2+3: you have put it as an activity now. remove the activity, the attribute must go into the application tag.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.multitabcheckboxselection"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:name="com.example.multitabcheckboxselection.MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.multitabcheckboxselection.BreakfastLunchDinnerIndividualListOfItems"
        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.multitabcheckboxselection.BLD_IndividualListOfItems_Starters" />
    <activity android:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_MainCourse" />
    <activity android:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_SideCourse" />
    <activity android:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Others" />
    <activity android:name="com.example.multitabcheckboxselection.BLD_IndividualListOfItems_Desert" />
    <activity android:name="com.example.multitabcheckboxselection.ResultActivity" />
</application>

</manifest>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...