我正在尝试运行一个从 0 计数到 1000 并停止的简单服务。这是我的代码:
public class MyService extends Service
{
private Timer timer;
private int counter;
public void onCreate()
{
super.onCreate();
this.timer = new Timer();
this.counter = 0;
startService();
}
private void startService()
{
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
MyService.this.counter++;
if(counter == 1000)
timer.cancel();
}
},0,100);
;}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
从我对 onCreate 方法的活动中,我做了以下事情: MyService service = new MyService();
我的应用程序崩溃了,我收到以下错误:
01-05 16:34:28.880: E/AndroidRuntime(30183): java.lang.RuntimeException: 无法实例化活动 ComponentInfo{com.example.android.location/com.example.android.location.MainActivity}: java.lang .ClassNotFoundException: com.example.android.location.MainActivity
编辑
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.location"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application android:label="@string/app_name" >
<service android:name="com.example.android.location.MyService" />
<activity
android:name="com.example.android.location.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
编辑
这是活动代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}