我的onclick监听器不会运行我正在使用的按钮,它会返回错误,我的应用程序在我的虚拟设备上关闭。我在下面的类中创建了按钮,并在我的清单中列出了这些代码
02-15 06:44:07.246: E/AndroidRuntime(837): FATAL EXCEPTION: main
02-15 06:44:07.246: E/AndroidRuntime(837): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.kingstonrent.uniproject/com.kingstonrent.uniproject.rentcalculator}: java.lang.ClassNotFoundException: Didn't find class "com.kingstonrent.uniproject.rentcalculator" on path: DexPathList[[zip file "/data/app/com.kingstonrent.uniproject-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.kingstonrent.uniproject-2, /system/lib]]
02-15 06:44:07.246: E/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
02-15 06:44:07.246: E/AndroidRuntime(837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-15 06:44:07.246: E/AndroidRuntime(837): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-15 06:44:07.246: E/AndroidRuntime(837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-15 06:44:07.246: E/AndroidRuntime(837): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 06:44:07.246: E/AndroidRuntime(837): at android.os.Looper.loop(Looper.java:137)
02-15 06:44:07.246: E/AndroidRuntime(837): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-15 06:44:07.246: E/AndroidRuntime(837): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 06:44:07.246: E/AndroidRuntime(837): at java.lang.reflect.Method.invoke(Method.java:525)
02-15 06:44:07.246: E/AndroidRuntime(837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-15 06:44:07.246: E/AndroidRuntime(837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-15 06:44:07.246: E/AndroidRuntime(837): at dalvik.system.NativeStart.main(Native Method)
02-15 06:44:07.246: E/AndroidRuntime(837): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.kingstonrent.uniproject.rentcalculator" on path: DexPathList[[zip file "/data/app/com.kingstonrent.uniproject-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.kingstonrent.uniproject-2, /system/lib]]
02-15 06:44:07.246: E/AndroidRuntime(837): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
02-15 06:44:07.246: E/AndroidRuntime(837): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-15 06:44:07.246: E/AndroidRuntime(837): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-15 06:44:07.246: E/AndroidRuntime(837): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-15 06:44:07.246: E/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
02-15 06:44:07.246: E/AndroidRuntime(837): ... 11 more
清单
<activity
android:name="com.kingstonrent.uniproject.rentcalculator"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.kingstonrent.uniproject.RENTCALCULATOR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
menu.java
public class menu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sr1 = (Button) findViewById(R.id.search);
Button tut2 = (Button) findViewById(R.id.Notesone);
Button tut3 = (Button) findViewById(R.id.twittertwo);
Button but4 = (Button) findViewById(R.id.button1);
sr1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.kingstonrent.uniproject.BROWSER"));
}
});
tut2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.kingstonrent.uniproject.MAKENOTE"));
}
});
tut3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.kingstonrent.uniproject.TWEET"));
}
});
but4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.kingstonrent.uniproject.RENTCALCULATOR"));
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menuone:
startActivity(new Intent("com.kingstonrent.uniproject.MENUBUTTON"));
return true;
}
return false;
}
}
答案 0 :(得分:1)
在Android [活动]中转入另一个屏幕。
明确的意图
startActivity(new Intent (menu.this,com.kingstonrent.uniproject.BROWSER.class));
其中明确提到哪个活动转移到。但是对于这种情况,您需要当前活动的上下文,您可以从中启动转移到的Transit和Class名称。
在你的情况下
您当前活动的背景是menu.this
您现在所处的位置。请适当使用命名约定和标准,因为menu
是类。您应该使用Menu
代替menu
。
转化费用为com.kingstonrent.uniproject.BROWSER.class
,应在活动名称后附加.class
文件时提及。
您还需要在Yout Manifest文件中提及您转移到的活动。
<application>
<activity
android:name="com.kingstonrent.uniproject.BROWSER"/>
<activity
android:name="com.kingstonrent.uniproject.rentcalculator"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.kingstonrent.uniproject.RENTCALCULATOR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
答案 1 :(得分:0)
调用意图类的方法
Intent instance = new Intent(this,Activity.class);
startActivity(new Intent(“com.kingstonrent.uniproject.BROWSER”));
替换为
startActivity(new Intent(menu.this,com.kingstonrent.uniproject.BROWSER.class));
与其他声明的意图相同