这是我的Activity类中的代码部分。这是onListItemClick
方法。我使用String数组输入菜单活动类名。 classes
是数组。
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.example.image_and_sound_app."+cheese);
Intent ourIntent = new Intent(Menu.this,ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
但是当我点击一个列表项时,会发生这样的异常
nd_sound_app.app W/System.err﹕ Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.image_and_sound_app.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.image_and_sound_app.app-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.image_and_sound_app.app-2, /system/lib]]
07-09 15:50:14.220 1288-1288/com.example.image_and_sound_app.app W/System.err﹕ at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-09 15:50:14.220 1288-1288/com.example.image_and_sound_app.app W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
07-09 15:50:14.220 1288-1288/com.example.image_and_sound_app.app W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
已经存在MainActivity
类。但是例外表明找不到类。我该如何解决这个问题?
答案 0 :(得分:0)
所以你给错了类名。 MainActivity类名称不是:com.example.image_and_sound_app.MainActivity。 仔细检查主要活动的所有包名称。 不能做任何其他事情。
答案 1 :(得分:0)
很抱歉,如果我不使用评论选项,我的代表太低了。我知道如何帮助你......
我看到你在buckysroom上做了Android教程。
您在上述评论中所写的内容并非主要活动的声明。您只需将链接构建到活动本身。
String classes[] = {"MainActivity", "TextPlay", "Email", "Notebook", "example4", "example5", "example6" };
这一行只是一个后来的代码构建链接的数组。
Class ourClass = null;
try {
ourClass = Class.forName("com.awo.travistut."+links);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
这是缺少的主要类:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SoundEffectConstants;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bDelete);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter = counter+1;
display.setText("Your total is " + counter);
v.playSoundEffect(SoundEffectConstants.CLICK);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter = counter-1;
display.setText("Your total is " + counter);
v.playSoundEffect(SoundEffectConstants.CLICK);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause(){
super.onPause();
finish();
}
}
这是构建导航的菜单代码:
public class Menu extends ListActivity {
String classes[] = {"MainActivity", "TextPlay", "Email", "Notebook", "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);
String links = classes[position];
Class ourClass = null;
try {
ourClass = Class.forName("com.awo.travistut."+links);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
答案 2 :(得分:0)
为什么你们将解决方法拉到简单的问题?您可以采用更高效,更可靠的方式,而不是使用反射。这是你的代码:
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.example.image_and_sound_app."+cheese);
Intent ourIntent = new Intent(Menu.this,ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
让我们假设类是:
private static final String[] classes = {
"A",
"B",
"C"
};
等等。不是动态构建类,而是构建它们!
private static final Class<?>[] classes = {
A.class,
B.class,
C.class
};
...
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id); // this line is redundant
Class<?> ourClass = classes[position];
Intent ourIntent = new Intent(Menu.this,ourClass);
startActivity(ourIntent);
}
还有一个很好的导航处理模式,但我不知道你是否会使用它。