简体   繁体   English

我在使用Android中的类对象启动活动时遇到问题

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

So I'm following thenewboston's Android tutorials. 因此,我正在关注thenewboston的Android教程。 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. 但是,使用类对象创建新活动时,无法在Class.forname()方法中识别类名称。 But say if I created directly using Intent, it has no problem finding class. 但是要说如果我直接使用Intent创建的话,查找类就没有问题了。 But using this, with same class path, I'm having java.lang.ClassNotFoundException` package name- com.hello.myproject; 但是使用这个,在相同的类路径下,我有java.lang.ClassNotFoundException`包名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>

Narayan Payyoor 纳拉扬·佩约尔(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. 仅当您在manifest.xml文件的Activity标记内添加了intent-filter时,您用于启动Activity的语法才有效。

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 您必须在清单文件中使用intent-filter声明此Activity

<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... 快乐编码...

You have to pass the name of .class where you want to go. 您必须在要传递的.class名称中传递。 Use like this: 像这样使用:

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

Use this 用这个

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

OR 要么

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

Define "MainActivity" inside Manifest xml file and the call like this: 在清单xml文件和调用中定义“ MainActivity”,如下所示:

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

hope it will work for you. 希望它对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM