0

When I run program then my emulator stops

1st main class name is MainActivity.java

public class MainActivity extends Activity {

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

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

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, AndroidExplorer.class));

            }
        });

    }

2nd class name is AndroidExplorer.java

public class AndroidExplorer extends ListActivity 
{

    private List<String> item = null;
    private List<String> path = null;
    private String root="/";
    private TextView myPath;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myPath = (TextView)findViewById(R.id.path);
        getDir(root);
    }

    private void getDir(String dirPath)
    {
        myPath.setText("Location: " + dirPath);

        item = new ArrayList<String>();
        path = new ArrayList<String>();

        File f = new File(dirPath);
        File[] files = f.listFiles();

        if(!dirPath.equals(root))
        {

            item.add(root);
            path.add(root);

            item.add("../");
            path.add(f.getParent());

        }

        for(int i=0; i < files.length; i++)
        {
                File file = files[i];
                path.add(file.getPath());
                if(file.isDirectory())
                    item.add(file.getName() + "/");
                else
                    item.add(file.getName());
        }

        ArrayAdapter<String> fileList =
            new ArrayAdapter<String>(this, R.layout.row, item);
        setListAdapter(fileList);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        File file = new File(path.get(position));

        if (file.isDirectory())
        {
            if(file.canRead())
                getDir(path.get(position));
            else
            {
                new AlertDialog.Builder(this)
                .setIcon(R.drawable.icon)
                .setTitle("[" + file.getName() + "] folder can't be read!")
                .setPositiveButton("OK", 
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                            }
                        }).show();
            }
        }
        else
        {
            new AlertDialog.Builder(this)
                .setIcon(R.drawable.icon)
                .setTitle("[" + file.getName() + "]")
                .setPositiveButton("OK", 
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                            }
                        }).show();
        }
    }}

Logcat Output:

 06-12 00:40:17.718: E/AndroidRuntime(12701): FATAL EXCEPTION: main
 06-12 00:40:17.718: E/AndroidRuntime(12701): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp.ftpfilesharing/com.fyp.ftpfilesharing.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout 
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
  • post stacktrace from Logcat – mt0s Jun 11 '13 at 19:37
  • 06-12 00:40:17.718: E/AndroidRuntime(12701): FATAL EXCEPTION: main 06-12 00:40:17.718: E/AndroidRuntime(12701): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp.ftpfilesharing/com.fyp.ftpfilesharing.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout – user2465852 Jun 11 '13 at 19:42
  • did you declare your Activity in `Manifest` file ? – mt0s Jun 11 '13 at 19:43
  • yes i declared my activity in Maifest file also – user2465852 Jun 11 '13 at 19:44
  • Please edit your question to include the full stack trace, properly formatted. – Simon Jun 11 '13 at 19:46
  • @Simon stack trace u mean xml and manifest files ? – user2465852 Jun 11 '13 at 19:48
  • logcat output with error. Not just a line – mt0s Jun 11 '13 at 19:51
  • 1
    http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Simon Jun 11 '13 at 19:52
  • 06-12 00:40:17.718: E/AndroidRuntime(12701): FATAL EXCEPTION: main 06-12 00:40:17.718: E/AndroidRuntime(12701): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp.ftpfilesharing/com.fyp.ftpfilesharing.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout 06-12 00:40:17.718: E/AndroidRuntime(12701): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872) 06-12 00:40:17.718: E/AndroidRuntime(12701): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) – user2465852 Jun 11 '13 at 20:03
  • As I asked, please edit your question. It's unreadable in a comment. – Simon Jun 11 '13 at 20:07
  • brother i can find the error myself and thanks for helping me :) – user2465852 Jun 11 '13 at 20:17

1 Answers1

0

I'm pretty sure in your manifest your activity isn't a ListActivity but a RelativeLayout instead. Still, for clarity, it wouldn't hurt if you posted both your layout and your logcat (a couple more lines than those you've already posted)

DigCamara
  • 5,540
  • 4
  • 36
  • 47