服务不工作(Services Don't work)
我需要在OnStartCommand方法中显示Toast,并在logCat中显示OnCreate方法。 这是主要类:
public class MainActivity extends ActionBarActivity { WebView webView; ProgressBar progressBar; String url = "http://www.amrp.tecnogeppetto.it/"; final Intent Service = new Intent(getBaseContext(),Services.class); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webView1); progressBar = (ProgressBar) findViewById(R.id.progressBar1); //new LoadUrl().execute(url); startService(Service); } }这是Services.java类:
import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class Services extends Service { public static final String TAG = "Services"; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void onCreate(){ Log.d(TAG,"onCreate"); } public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG,"onStartCommand"); Toast.makeText(this, "ooo", Toast.LENGTH_LONG).show(); return START_STICKY; } }我还在Manifest中提供服务:
.... <service android:name=".Services" /> </application> </manifest>帮我。 谢谢
编辑:错误日志
09-26 14:34:34.059: E/AndroidRuntime(1472): FATAL EXCEPTION: main 09-26 14:34:34.059: E/AndroidRuntime(1472): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.amrp/com.example.amrp.MainActivity}: java.lang.NullPointerException 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread.access$600(ActivityThread.java:122) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.os.Handler.dispatchMessage(Handler.java:99) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.os.Looper.loop(Looper.java:137) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread.main(ActivityThread.java:4340) 09-26 14:34:34.059: E/AndroidRuntime(1472): at java.lang.reflect.Method.invokeNative(Native Method) 09-26 14:34:34.059: E/AndroidRuntime(1472): at java.lang.reflect.Method.invoke(Method.java:511) 09-26 14:34:34.059: E/AndroidRuntime(1472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 09-26 14:34:34.059: E/AndroidRuntime(1472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 09-26 14:34:34.059: E/AndroidRuntime(1472): at dalvik.system.NativeStart.main(Native Method) 09-26 14:34:34.059: E/AndroidRuntime(1472): Caused by: java.lang.NullPointerException 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.content.ComponentName.<init>(ComponentName.java:75) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.content.Intent.<init>(Intent.java:3004) 09-26 14:34:34.059: E/AndroidRuntime(1472): at com.example.amrp.MainActivity.<init>(MainActivity.java:24) 09-26 14:34:34.059: E/AndroidRuntime(1472): at java.lang.Class.newInstanceImpl(Native Method) 09-26 14:34:34.059: E/AndroidRuntime(1472): at java.lang.Class.newInstance(Class.java:1319) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870) 09-26 14:34:34.059: E/AndroidRuntime(1472): ... 11 moreI need to show a Toast in the OnStartCommand method and show in the logCat the OnCreate method. this is the main class:
public class MainActivity extends ActionBarActivity { WebView webView; ProgressBar progressBar; String url = "http://www.amrp.tecnogeppetto.it/"; final Intent Service = new Intent(getBaseContext(),Services.class); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webView1); progressBar = (ProgressBar) findViewById(R.id.progressBar1); //new LoadUrl().execute(url); startService(Service); } }and this the Services.java class:
import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class Services extends Service { public static final String TAG = "Services"; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void onCreate(){ Log.d(TAG,"onCreate"); } public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG,"onStartCommand"); Toast.makeText(this, "ooo", Toast.LENGTH_LONG).show(); return START_STICKY; } }I also delcared service in the Manifest as :
.... <service android:name=".Services" /> </application> </manifest>Help me. Thanks
EDIT: ERROR LOG
09-26 14:34:34.059: E/AndroidRuntime(1472): FATAL EXCEPTION: main 09-26 14:34:34.059: E/AndroidRuntime(1472): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.amrp/com.example.amrp.MainActivity}: java.lang.NullPointerException 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread.access$600(ActivityThread.java:122) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.os.Handler.dispatchMessage(Handler.java:99) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.os.Looper.loop(Looper.java:137) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread.main(ActivityThread.java:4340) 09-26 14:34:34.059: E/AndroidRuntime(1472): at java.lang.reflect.Method.invokeNative(Native Method) 09-26 14:34:34.059: E/AndroidRuntime(1472): at java.lang.reflect.Method.invoke(Method.java:511) 09-26 14:34:34.059: E/AndroidRuntime(1472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 09-26 14:34:34.059: E/AndroidRuntime(1472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 09-26 14:34:34.059: E/AndroidRuntime(1472): at dalvik.system.NativeStart.main(Native Method) 09-26 14:34:34.059: E/AndroidRuntime(1472): Caused by: java.lang.NullPointerException 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.content.ComponentName.<init>(ComponentName.java:75) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.content.Intent.<init>(Intent.java:3004) 09-26 14:34:34.059: E/AndroidRuntime(1472): at com.example.amrp.MainActivity.<init>(MainActivity.java:24) 09-26 14:34:34.059: E/AndroidRuntime(1472): at java.lang.Class.newInstanceImpl(Native Method) 09-26 14:34:34.059: E/AndroidRuntime(1472): at java.lang.Class.newInstance(Class.java:1319) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 09-26 14:34:34.059: E/AndroidRuntime(1472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870) 09-26 14:34:34.059: E/AndroidRuntime(1472): ... 11 more最满意答案
您需要在MainActivity类中删除这些声明:
final Intent Service = new Intent(getBaseContext(), Services.class); startService(Service);并替换为:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Other codes here. Intent service = new Intent(this, Services.class); startService(service); }在您的Services类中,执行以下操作:
public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG,"onStartCommand"); Toast.makeText(getApplicationContext(), "ooo", Toast.LENGTH_LONG).show(); return START_STICKY; }不要使用getBaseContext() 。
You need to get rid of these declarations in your MainActivity class:
final Intent Service = new Intent(getBaseContext(), Services.class); startService(Service);And replaced them with:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Other codes here. Intent service = new Intent(this, Services.class); startService(service); }And in your Services class, do this:
public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG,"onStartCommand"); Toast.makeText(getApplicationContext(), "ooo", Toast.LENGTH_LONG).show(); return START_STICKY; }Don't use getBaseContext().
更多推荐
发布评论