AntiNational
AntiNational

Reputation: 47

I'm having trouble starting an activity using class object in Android

So I'm following thenewboston's Android tutorials. I understood how to start an activity using intent. But when it comes to creating a new activity using class object the class name is not being recognized inside the Class.forname()method. But say if I created directly using Intent, it has no problem finding class. But using this, with same class path, I'm having java.lang.ClassNotFoundException` package name- com.hello.myproject;

  import android.app.ListActivity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;


public class Menu extends ListActivity{
    String []classes={"MainActivity","example1","example2","example3","example4","example5","example6"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        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) {
        super.onListItemClick(l, v, position, id);

        Class myclass=Class.forName("com.hello.myproject.MainActivity");//This class name is not being found, but if I copy the same path inside Intent() it has no problem and yes there is a class called MainActivity
        Intent myintent=new Intent(Menu.this,myclass );
        startActivity(myintent);
    }
}

Manifestfile

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".splash">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="com.hello.myproject.MAINACTIVITY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".Menu">
        <intent-filter>
            <action android:name="com.hello.myproject.MENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".TextPlay">
        <intent-filter>
            <action android:name="com.hello.myproject.TEXTPLAY" />

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

Upvotes: 0

Views: 167

Answers (4)

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

Narayan Payyoor

The syntax you are using for starting Activity works only if you have added intent-filter inside Activity tag in manifest.xml file.

For Example :

if you are doing,

Class myclass = Class.forName("com.hello.myproject.MAINACTIVITY");
Intent myintent = new Intent(Menu.this,myclass );
startActivity(myintent);

you must declare this Activity in manifest file with intent-filter

<activity
    android:name="com.hello.myproject.MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait" >
    <intent-filter>
        <action android:name="com.hello.myproject.MAINACTIVITY" />

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

OR

As every on suggested, you can just use class name.

Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);

Update :

As you want to open different different Activities on List Item Click, here how you can do.. I am not sure but it should work.

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

    Class myclass = Class.forName("com.hello.myproject." + classes[position]);
    Intent myintent = new Intent(Menu.this,myclass );
    startActivity(myintent);
}

Happy Coding...

Upvotes: 1

oldcode
oldcode

Reputation: 1711

Use this

Intent myintent=new Intent(Menu.this, MainActivity.class);

OR

    Class myclass=Class.forName("MainActivity");
Intent myintent=new Intent(Menu.this, myclass);

Upvotes: 0

prem nath
prem nath

Reputation: 75

Define "MainActivity" inside Manifest xml file and the call like this:

Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);

hope it will work for you.

Upvotes: 0

W4R10CK
W4R10CK

Reputation: 5550

You have to pass the name of .class where you want to go. Use like this:

Intent myintent=new Intent(Menu.this, MainActivity.class);

Upvotes: 0

Related Questions