1

I'm kind of new to android, actually this is my first app. The app is not completed through, i wanted to test the UI, it works fine on AVD, but when I copy the .apk file from bin folder to my phone, it installs fine, but wont work ends up with force close error. I have no idea what's the problem, I saw on other post about parsing the content in edittext, i took out the layout and ran it again still not working. again, I am new to this, please help me.

Mainactivity.java

package com.example.studentapp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

import com.example.studentapp.ExpandableListAdapter;

import android.widget.Toast;

 public class MainActivity extends ActionBarActivity {

    DrawerLayout mDrawerLayout;
    ExpandableListView mDrawerList;
    ExpandableListAdapter listAdapter;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    private ActionBarDrawerToggle actionBarDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getfragment(125);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);



            // preparing list data
            prepareListData();

            listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

            // setting list adapter
            mDrawerList.setAdapter(listAdapter);

            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            actionBarDrawerToggle = new ActionBarDrawerToggle(
                    this,                  /* host Activity */
                    mDrawerLayout,         /* DrawerLayout object */
                    R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                    R.string.drawer_open,  /* "open drawer" description */
                    R.string.drawer_close  /* "close drawer" description */
                    );

            // 2.2 Set actionBarDrawerToggle as the DrawerListener
           mDrawerLayout.setDrawerListener(actionBarDrawerToggle);

            // 2.3 enable and show "up" arrow
            getActionBar().setDisplayHomeAsUpEnabled(true); 

            // Listview Group click listener
            mDrawerList.setOnGroupClickListener(new OnGroupClickListener() {

                @Override
                public boolean onGroupClick(ExpandableListView parent, View v,
                        int groupPosition, long id) {
                     Toast.makeText(getApplicationContext(),
                     "Group Clicked " + (groupPosition),
                     Toast.LENGTH_SHORT).show();

                     //return true for non collapsable items, and false for collapsable items.
                     if(groupPosition==1 || groupPosition==2)
                     { // getfragment(groupPosition);
                     getActionBar().setTitle(listDataHeader.get(groupPosition));
                    return true;}
                     else
                         return false;
                }
            });

            // Listview Group expanded listener
            mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            listDataHeader.get(groupPosition) + " Expanded",
                            Toast.LENGTH_SHORT).show();
                }
            });

            // Listview Group collasped listener
            mDrawerList.setOnGroupCollapseListener(new OnGroupCollapseListener() {

                @Override
                public void onGroupCollapse(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            listDataHeader.get(groupPosition) + " Collapsed",
                            Toast.LENGTH_SHORT).show();

                }
            });

            // Listview on child click listener
            mDrawerList.setOnChildClickListener(new OnChildClickListener() {

                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                    // TODO Auto-generated method stub
                    Toast.makeText(
                            getApplicationContext(),
                            listDataHeader.get(groupPosition)
                                    + " : "
                                    + listDataChild.get(
                                            listDataHeader.get(groupPosition)).get(
                                            childPosition), Toast.LENGTH_SHORT)
                            .show();

                    getActionBar().setTitle(listDataChild.get(
                            listDataHeader.get(groupPosition)).get(
                            childPosition));

                    return false;
                }
            });







        }

    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

         // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
        // then it has handled the app icon touch event

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding header data
        listDataHeader.add("Center Details");

        // Adding child data
        List<String> CentDet = new ArrayList<String>();
        CentDet.add("Study Center");
        CentDet.add("Information Center");
        CentDet.add("Overseas Center");
        listDataHeader.add("Login");
        listDataHeader.add("PCP Schedule");
        listDataHeader.add("Course");
        List<String> courseS = new ArrayList<String>();
        courseS.add("Study Materials");
        courseS.add("Syllabus");


        listDataHeader.add("Exam");
        List<String> Exams = new ArrayList<String>();
        Exams.add("Hall Ticket");
        Exams.add("Exam Venue");
        Exams.add("Exam Schedule");
        Exams.add("Practical Schedule");
        Exams.add("Model Question Papers");

listDataHeader.add("Results");
List<String> Resultss= new ArrayList<String>();
Resultss.add("Regular Stream");
Resultss.add("Overseas");
        listDataChild.put(listDataHeader.get(0), CentDet); // Header, Child data
        listDataChild.put(listDataHeader.get(1), null);
        listDataChild.put(listDataHeader.get(2), null);
        listDataChild.put(listDataHeader.get(3), courseS);
        listDataChild.put(listDataHeader.get(4), Exams);
        listDataChild.put(listDataHeader.get(5), Resultss);

    }




  void getfragment(int x)
  {
      android.app.FragmentManager fragmentManager1 = getFragmentManager();
        android.app.FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
    fragmenttwo lm_fragment = new fragmenttwo();
    switch (x)
    {

 //     case 1:
  //    lm_fragment.fragmenttwoo(1);
  //    
    //break;

    case 2:
        lm_fragment.fragmenttwoo(2);

    break;

    case 3:
        lm_fragment.fragmenttwoo(3);
    break;



    case 125:
        lm_fragment.fragmenttwoo(125);
        break;

    }       
        fragmentTransaction1.replace(R.id.content_frame, lm_fragment);
        fragmentTransaction1.commit();

    }


}

fragmenttwo.java

 package com.example.studentapp;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class fragmenttwo extends Fragment{ 

    public int x;

    public fragmenttwo(){}

    public  void fragmenttwoo(int n)
    {
        switch(n)
        {
        case 125:
            x = com.example.studentapp.R.layout.sample;
            break;

    //  case 1:
        //  x = com.example.studentapp.R.layout.login;
        //  break;

        case 2:
            x = com.example.studentapp.R.layout.pcpschedule;
            break;
        }
    }
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        /** * Inflate the layout for this fragment */
    //  switch(x)
    //  {
        // int idno= r.layout.layoutu want for each case
        //put the idno on below as parameter instead of r.layout.sample
        //}


        return inflater.inflate( x, container, false); 
        } 
    }

ExpandableListAdpater.java

    package com.example.studentapp;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

any help would be appreciated.

As suggested i used adb and here is the logcat log, it says virtual method getfragment cannot be resolved...

06-03 21:18:03.789: W/dalvikvm(11126): VFY: unable to resolve virtual method 8281: Lcom/example/studentapp/MainActivity;.getFragmentManager ()Landroid/app/FragmentManager;
06-03 21:18:03.789: D/dalvikvm(11126): VFY: replacing opcode 0x6e at 0x0000
06-03 21:18:03.789: D/dalvikvm(11126): VFY: dead code 0x0003-0029 in Lcom/example/studentapp/MainActivity;.getfragment (I)V
06-03 21:18:03.812: W/dalvikvm(11126): VFY: unable to resolve virtual method 8279: Lcom/example/studentapp/MainActivity;.getActionBar ()Landroid/app/ActionBar;
06-03 21:18:03.812: D/dalvikvm(11126): VFY: replacing opcode 0x6e at 0x005b
06-03 21:18:03.812: D/dalvikvm(11126): VFY: dead code 0x005e-008b in Lcom/example/studentapp/MainActivity;.onCreate (Landroid/os/Bundle;)V
06-03 21:18:03.820: I/ApplicationPackageManager(11126): cscCountry is not German : INS
06-03 21:18:03.976: D/AndroidRuntime(11126): Shutting down VM
06-03 21:18:03.976: W/dalvikvm(11126): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-03 21:18:03.976: E/AndroidRuntime(11126): FATAL EXCEPTION: main
06-03 21:18:03.976: E/AndroidRuntime(11126): java.lang.NoSuchMethodError: com.example.studentapp.MainActivity.getFragmentManager
06-03 21:18:03.976: E/AndroidRuntime(11126):    at com.example.studentapp.MainActivity.getfragment(MainActivity.java:223)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at com.example.studentapp.MainActivity.onCreate(MainActivity.java:42)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at android.os.Looper.loop(Looper.java:130)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at java.lang.reflect.Method.invoke(Method.java:507)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-03 21:18:03.976: E/AndroidRuntime(11126):    at dalvik.system.NativeStart.main(Native Method)
Kara
  • 6,115
  • 16
  • 50
  • 57
  • Why dont you use the [ADB](http://developer.android.com/tools/help/adb.html)? – Skynet Jun 03 '14 at 13:51
  • Look in LogCat, there you should find the reason that caused the crash and the line number that creates it. Here's a blog post that show how to debug app crashes: http://www.itcsolutions.eu/2011/09/11/android-tutorial-7-how-to-debug-the-android-mobile-application-with-logcat/ – Andy Res Jun 03 '14 at 13:51
  • @AndyRes for that the OP needs to first connect the device to the System -- via ADB. – Skynet Jun 03 '14 at 13:52
  • 1
    `when I copy the .apk file from bin folder to my phone,`that is not the normal way you would debug an app. Normally you would connect the phone with a usb cable to your pc. Then in Eclipse you would right click on your application folder and choose `Run As | Android Application` Eclipse would then install the app on your phone and run it. Did you try that? That's what the others call `using adb`. – greenapps Jun 03 '14 at 14:12

1 Answers1

0

Read the logcat: noSuchMethodError on line 223. Double click that line in the logcat and you will end up on line 223 of your MainActivity. Tell us the code.

CscCountry is not German?

Or you are running on a device that does not know getActionBar().

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • I solved the getActionBar() by importing the support.v4.app.ActionBar and changed it to getSupportActionBar(). Now I am having Problem on replace method on fragmenttransaction, I also imported ...v4.FragmentTransaction. it says" The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, fragmenttwo)" – Mahab Phoenix Khan Jun 03 '14 at 17:39
  • Try to find out what arguments it needs and of which type. Eclipse wil tell you as soon as you type in the . before .replace(). – greenapps Jun 03 '14 at 17:46