当我通过onClickListener单击一个菜单项时,它表示该类不存在,尽管它存在。我如何解决它?

时间:2016-05-01 12:11:21

标签: java android xml

这是我的菜单类

package devgan.jatin.com.myapplication;

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 {
private final String[] classes={"StartingPoint","GFX"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<>(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);
    String cheese=classes[position];


    try {
        Class ourClass = Class.forName("com.jatin.devgan."+cheese);
        Intent ourIntent = new Intent(Menu.this, ourClass);
        startActivity(ourIntent);

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    }
}

这是我的清单

<?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=".StartingPoint">
        <intent-filter>
            <action android:name="com.jatin.devgan.STARTINGPOINT" />

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

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

这就是我的所作所为。创建一个启动画面,进入菜单。 您可以看到菜单类。当我打算通过菜单启动StartingPoint活动时,它会给我ClassNotFoundException -

但事情是,当我调整我的代码直接从启动画面到StartingPoint时,它工作正常!我很困惑。 所有软件都是最新的。

1 个答案:

答案 0 :(得分:0)

为什么不实施切换方法,根据交换机启动活动?

    ListView lv = findViewById(R.id.LV);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view,int position, long l) {
            switch (position)
            {
                case 1 : 
                {
                    Intent intent = new Intent(getApplicationContext(),OtherClass.class);
                    startActivity(intent);
                }break;
                case 2: 
                {
                    Intent intent = new Intent(getApplicationContext(),OtherClass.class);
                    startActivity(intent);
                }
                break;
            }
        }
    });