繁体   English   中英

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

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

因此,我正在关注thenewboston的Android教程。 我了解如何使用意图开始活动。 但是,使用类对象创建新活动时,无法在Class.forname()方法中识别类名称。 但是要说如果我直接使用Intent创建的话,查找类就没有问题了。 但是使用这个,在相同的类路径下,我有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);
    }
}

清单文件

<?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)

仅当您在manifest.xml文件的Activity标记内添加了intent-filter时,您用于启动Activity的语法才有效。

例如 :

如果你在做,

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

您必须在清单文件中使用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>

要么

正如每个建议一样,您可以只使用类名。

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

更新:

当您想在“列表项单击”上打开不同的“活动”时,这里将如何操作。.我不确定,但是应该可以。

@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);
}

快乐编码...

您必须在要传递的.class名称中传递。 像这样使用:

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

用这个

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

要么

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

在清单xml文件和调用中定义“ MainActivity”,如下所示:

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

希望它对您有用。

暂无
暂无

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

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