我试图在Android中编写代码,但我无法在外部设备上运行。它总是强行退出。我在android中编写了一两件事,我总是能够启动它们,所以我不知道这个错误来自哪里。
这是启动器活动的代码:
package sander.bylemans.alarm;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
final int setAlarm = 214;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this,AlarmScreen.class), setAlarm);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == setAlarm){
if(resultCode == RESULT_OK){
String name = data.getStringExtra("Name");
String time = data.getStringExtra("Time"); //In h:m format
String days = data.getStringExtra("Days"); //Seperated by ;
String[] t = time.split(":");
String[] d = days.split(";");
int[] hrsAndMin = new int[t.length];
for(int i = 0; i < t.length; i++){
hrsAndMin[i] = Integer.parseInt(t[i]);
}
ArrayList<Day> ds = new ArrayList<Day>();
for(int i = 0; i < d.length; i++){
switch(d[i]){
case("Monday"):
ds.add(Day.Monday);
case("Tuesday"):
ds.add(Day.Tuesday);
case("Wednesday"):
ds.add(Day.Wednesday);
case("Thursday"):
ds.add(Day.Thursday);
case("Friday"):
ds.add(Day.Friday);
case("Saturday"):
ds.add(Day.Saturday);
case("Sunday"):
ds.add(Day.Sunday);
}
}
Alarm alarm = new Alarm(name, hrsAndMin[0], hrsAndMin[1] , Status.ON, ds);
addAlarm(alarm);
}
}
}
@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);
}
private void addAlarm(Alarm alarm){
}
}
这是AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sander.bylemans.alarm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="sander.bylemans.alarm.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="sander.bylemans.alarm.AlarmScreen"
android:label="@string/title_activity_alarm_screen" >
</activity>
</application>
</manifest>
这是我收到的错误,我不知道如何解决:
10-27 20:56:52.278: W/dalvikvm(14367): Unable to resolve superclass of Lsander/bylemans/alarm/MainActivity; (4)
10-27 20:56:52.278: W/dalvikvm(14367): Link of class 'Lsander/bylemans/alarm/MainActivity;' failed
10-27 20:56:52.298: W/dalvikvm(14367): threadid=1: thread exiting with uncaught exception (group=0x4001d560)
10-27 20:56:52.398: E/AndroidRuntime(14367): FATAL EXCEPTION: main
10-27 20:56:52.398: E/AndroidRuntime(14367): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{sander.bylemans.alarm/sander.bylemans.alarm.MainActivity}: java.lang.ClassNotFoundException: sander.bylemans.alarm.MainActivity in loader dalvik.system.PathClassLoader[/data/app/sander.bylemans.alarm-2.apk]
10-27 20:56:52.398: E/AndroidRuntime(14367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
10-27 20:56:52.398: E/AndroidRuntime(14367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-27 20:56:52.398: E/AndroidRuntime(14367): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-27 20:56:52.398: E/AndroidRuntime(14367): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-27 20:56:52.398: E/AndroidRuntime(14367): at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 20:56:52.398: E/AndroidRuntime(14367): at android.os.Looper.loop(Looper.java:130)
10-27 20:56:52.398: E/AndroidRuntime(14367): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-27 20:56:52.398: E/AndroidRuntime(14367): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 20:56:52.398: E/AndroidRuntime(14367): at java.lang.reflect.Method.invoke(Method.java:507)
10-27 20:56:52.398: E/AndroidRuntime(14367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-27 20:56:52.398: E/AndroidRuntime(14367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-27 20:56:52.398: E/AndroidRuntime(14367): at dalvik.system.NativeStart.main(Native Method)
10-27 20:56:52.398: E/AndroidRuntime(14367): Caused by: java.lang.ClassNotFoundException: sander.bylemans.alarm.MainActivity in loader dalvik.system.PathClassLoader[/data/app/sander.bylemans.alarm-2.apk]
10-27 20:56:52.398: E/AndroidRuntime(14367): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-27 20:56:52.398: E/AndroidRuntime(14367): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-27 20:56:52.398: E/AndroidRuntime(14367): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-27 20:56:52.398: E/AndroidRuntime(14367): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-27 20:56:52.398: E/AndroidRuntime(14367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
10-27 20:56:52.398: E/AndroidRuntime(14367): ... 11 more
我长时间搜索一个解决方案,似乎无法找到解决问题的解决方案。我尝试了不同的东西,但没有一个能奏效。如果有人能帮助我,我将永远感激不尽。
迎接
答案 0 :(得分:0)
VM无法找到android.support.v7.app.ActionBarActivity;
检查支持库jar的位置。它应该在项目的libs文件夹中才能工作
答案 1 :(得分:0)
似乎所需的jar不在你的libs文件夹中。将jar复制到libs文件夹
您需要确保在以下路径中检查了lib
Go to Properties->Java Build Path->Order and Export and make sure the library is checked.