zu starten Ich habe einen Dienst in meiner Anwendung erstellt und die Funktion aufgerufen, die den Dienst mithilfe einer Schaltfläche mit OnClick in XML gestartet, aber die App abstürzt. einige Hilfe wird geschätzt. Paket slide.apptech.com.nav;nicht in der Lage, einen Dienst Fehler in Android
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.annotation.Nullable;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.design.widget.FloatingActionButton;
import java.util.ArrayList;
import java.util.Calendar;
import static java.util.Calendar.*;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static String TAG = MainActivity.class.getSimpleName();
private Intent myIntent;
Button Refresh;
TextView updated;
//related to main screen list view
ListView lv;
Context context;
ArrayList prgmName;
public static int [] prgmImages={R.drawable.temp,R.drawable.hum,R.drawable.press,R.drawable.amb,R.drawable.alt};
public static String [] prgmNameList={" Temperature: "," Humidity: "," Pressure: "," Ambient Light:"," Altitude: "};
//GetFromServer gs1=new GetFromServer();
public String [] prgmVal={"1","2","3","4","5"};
public static String [] prgmUnit={" °C"," %"," hPa"," lx"," m"};
//public String datentime=gs1.DateNTime[0];
String datentime;
//boolean variable to indicate if the arrays have been set or not
Boolean arraysset;
GetFromServer gs1;
public ArrayList<String> atList=new ArrayList<String>();
public ArrayList<String> dataList=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
//these lines overite all the warnings by android to restrict large tasks
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lv = (ListView) findViewById(R.id.listView);
updated=(TextView)findViewById((R.id.datentime));
Refresh = (Button) findViewById(R.id.refresh);
//block related to main screen list view
//creating a object from getfromserver class to give a request to server
gs1=new GetFromServer();
new loadSomeStuff().execute();
updated.setText(datentime);
prgmVal[0]=Double.toString(gs1.Temperature[0]);
prgmVal[1]=Double.toString(gs1.Humidity[0]);
prgmVal[2]=Double.toString(gs1.Pressure[0]);
prgmVal[3]=Double.toString(gs1.Ambient[0]);
prgmVal[4]=Double.toString(gs1.Altitude[0]);
datentime="Updated on :"+gs1.DateNTime[0];
context = this;
//this adapter is defined in custom adapter
// see class for all the constructors
lv.setAdapter(new CustomAdapter(MainActivity.this, MainActivity.prgmNameList, MainActivity.prgmImages, prgmVal, MainActivity.prgmUnit));
//code related to refresh button the main screen find xml in content_main
Refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(new Intent(getBaseContext(),service.class));
//Toast.makeText(context, "Please wait!", Toast.LENGTH_LONG).show();
gs1 = new GetFromServer();
gs1.sendData();
gs1.setArray();
//codeline to start athe service
prgmVal[0] = Double.toString(gs1.Temperature[0]);
prgmVal[1] = Double.toString(gs1.Humidity[0]);
prgmVal[2] = Double.toString(gs1.Pressure[0]);
prgmVal[3] = Double.toString(gs1.Ambient[0]);
prgmVal[4] = Double.toString(gs1.Altitude[0]);
String datentime = "Updated on :" + gs1.DateNTime[0];
updated.setText(datentime);
context = MainActivity.this;
lv.setAdapter(new CustomAdapter(MainActivity.this, MainActivity.prgmNameList, MainActivity.prgmImages, prgmVal, MainActivity.prgmUnit));
//Toast.makeText(context, "Refreshed!!!", Toast.LENGTH_LONG).show();
}
});
//this code is for a floation button see xml
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity.this,Faq.class);
startActivity(myIntent);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
//this is code related to back button in action bar to close drawer if open else go back to previous activity
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//app crash due to following code
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (id == R.id.liConnection) {
drawer.closeDrawer(GravityCompat.START);
Intent myIntent = new Intent(MainActivity.this, Connect.class);
startActivity(myIntent);
}
else if (id == R.id.liRanges){
drawer.closeDrawer(GravityCompat.START);
Intent myIntent = new Intent(MainActivity.this, Ranges.class);
startActivity(myIntent);
}
else if (id == R.id.liSupport) {
drawer.closeDrawer(GravityCompat.START);
Intent myIntent = new Intent(MainActivity.this, Support.class);
startActivity(myIntent);
}
else if (id == R.id.liAbout) {
drawer.closeDrawer(GravityCompat.START);
Intent myIntent = new Intent(MainActivity.this, AboutUs.class);
startActivity(myIntent);
}
else if (id == R.id.liRateus) {
drawer.closeDrawer(GravityCompat.START);
Intent myIntent = new Intent(MainActivity.this,Rateus.class);
startActivity(myIntent);
}
return true;
}
public void getPosition(int position) {
if (position == 0) {
//code specific to first list item
myIntent = new Intent(MainActivity.this, ChartTemperature.class); //replace connect classs with analysis
startActivity(myIntent);
}
else if (position == 1) {
//code specific to first list item
Intent myIntent = new Intent(MainActivity.this, ChartHumidity.class);
startActivity(myIntent);
}
else if (position == 2) {
//code specific to first list item
Intent myIntent = new Intent(MainActivity.this, ChartPressure.class);
startActivity(myIntent);
}
else if (position == 3) {
//code specific to first list item
Intent myIntent = new Intent(MainActivity.this, ChartAmbient.class);
startActivity(myIntent);
}
else if (position == 4) {
//code specific to first list item
Intent myIntent = new Intent(MainActivity.this, ChartAltitude.class);
startActivity(myIntent);
}
}
// Method to start the service
public void startService(View view) {
startService(new Intent(getBaseContext(), service.class));
}
//here we are creating a async task to reduce load on oncreate method
public class loadSomeStuff extends AsyncTask<String, Integer, Void> {
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
try {
gs1.sendData();
gs1.setArray();
prgmVal[0]=Double.toString(gs1.Temperature[0]);
prgmVal[1]=Double.toString(gs1.Humidity[0]);
prgmVal[2]=Double.toString(gs1.Pressure[0]);
prgmVal[3]=Double.toString(gs1.Ambient[0]);
prgmVal[4]=Double.toString(gs1.Altitude[0]);
datentime="Updated on :"+gs1.DateNTime[0];
arraysset = true;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
service.java
package slide.apptech.com.nav;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
/**
* Created by MOHIT on 17-04-2016.
*/
public class service extends Service{
//creating a server class object
GetFromServer gs1;
//creating a database object
database record;
String value;
boolean tempflag,pressflag,humflag,lightflag;
double temperature,humidity,pressure,light,max,min;
NotificationManager manager;
Notification myNotication;
Context context;
MainActivity main;
public final String UPDATE_DATA = "update";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Toast.makeText(context, "Service Started in onStartCommand", Toast.LENGTH_LONG).show();
if(intent.getAction().equals(UPDATE_DATA)){
Toast.makeText(context, "Service Started in onStartCommand", Toast.LENGTH_LONG).show();
//getandcheck();
}
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void getandcheck(){
//getting the values from server
gs1 = new GetFromServer();
gs1.sendData();
gs1.setArray();
//setting variables to latest sensor values
temperature = gs1.Temperature[0];
humidity = gs1.Humidity[0];
pressure = gs1.Pressure[0];
light = gs1.Ambient[0];
record.open();
//making function calls to setup flags
checktemp();
checkhum();
checkpress();
checklight();
//if any flag is true the it means that readings are out of range
if(tempflag == true){
String readings = ("Temperature : " + temperature +" °C");
final Notification.Builder builder = new Notification.Builder(service.this);
builder.setStyle(new Notification.BigTextStyle(builder)
.bigText(readings)
.setBigContentTitle("WSstation")
.setSummaryText("Alert!!!"))
.setContentTitle("WSstation")
.setContentText("Your Temperature Readings are out of range")
.setSmallIcon(R.drawable.auicon);
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, builder.build());
}
if(pressflag == true){
String readings = ("Pressure : " + pressure +" hpa");
final Notification.Builder builder = new Notification.Builder(service.this);
builder.setStyle(new Notification.BigTextStyle(builder)
.bigText(readings)
.setBigContentTitle("WSstation")
.setSummaryText("Alert!!!"))
.setContentTitle("WSstation")
.setContentText("Your Pressure Readings are out of range")
.setSmallIcon(R.drawable.auicon);
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, builder.build());
}
if(humflag == true){
String readings = ("Humidity : " + humidity +" %");
final Notification.Builder builder = new Notification.Builder(service.this);
builder.setStyle(new Notification.BigTextStyle(builder)
.bigText(readings)
.setBigContentTitle("WSstation")
.setSummaryText("Alert!!!"))
.setContentTitle("WSstation")
.setContentText("Your Humidity Readings are out of range")
.setSmallIcon(R.drawable.auicon);
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, builder.build());
}
if(lightflag == true){
String readings = ("Ambient Light : " + light +" lx");
final Notification.Builder builder = new Notification.Builder(service.this);
builder.setStyle(new Notification.BigTextStyle(builder)
.bigText(readings)
.setBigContentTitle("WSstation")
.setSummaryText("Alert!!!"))
.setContentTitle("WSstation")
.setContentText("Your Ambient Light Readings are out of range")
.setSmallIcon(R.drawable.auicon);
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, builder.build());
}
}
public void checktemp(){
//for temperature checking
value = record.getMax(1);
max = Double.parseDouble(value);
value = record.getMin(1);
min = Double.parseDouble(value);
if((temperature < min) || (temperature > max)){
tempflag = true;
}
else{
tempflag = false;
}
}
public void checkhum(){
//for humidity checking
value = record.getMax(3);
max = Double.parseDouble(value);
value = record.getMin(3);
min = Double.parseDouble(value);
if((humidity < min) || (humidity > max)){
humflag = true;
}
else{
humflag = false;
}
}
public void checkpress(){
//for pressure checking
value = record.getMax(2);
max = Double.parseDouble(value);
value = record.getMin(2);
min = Double.parseDouble(value);
if((pressure < min) || (pressure > max)){
pressflag = true;
}
else{
pressflag = false;
}
}
public void checklight(){
//for ambient light checking
value = record.getMax(4);
max = Double.parseDouble(value);
value = record.getMin(4);
min = Double.parseDouble(value);
if((light < min) || (light > max)){
lightflag = true;
}
else{
lightflag = false;
}
}
/*@Override
public void onCreate() {
super.onCreate();
Toast.makeText(context, "Service Started in oncreate", Toast.LENGTH_LONG).show();
}*/
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="slide.apptech.com.nav.MainActivity"
tools:showIn="@layout/app_bar_main">
<RelativeLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#004D40">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#004D40"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Home"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<!--<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text=" Computer Languages..." />
-->
<ListView
android:paddingTop="60dp"
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="376dp"
android:layout_above="@+id/datentime">
</ListView>
<TextView
android:paddingTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Updated:"
android:textColor="#FFEB3B"
android:id="@+id/datentime"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Refresh"
android:id="@+id/refresh"
android:layout_gravity="center_horizontal"
android:onClick="startService"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
logcat
04-20 20:15:18.623 27500-27500/slide.apptech.com.nav E/MotionRecognitionManager: mSContextService = null
04-20 20:15:18.628 27500-27500/slide.apptech.com.nav E/MotionRecognitionManager: motionService = [email protected]28c858d
04-20 20:15:24.578 27500-27500/slide.apptech.com.nav E/AndroidRuntime: FATAL EXCEPTION: main
Process: slide.apptech.com.nav, PID: 27500
java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=slide.apptech.com.nav/.service }: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3978)
at android.app.ActivityThread.access$2300(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at slide.apptech.com.nav.service.onStartCommand(service.java:33)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3961)
at android.app.ActivityThread.access$2300(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Ich habe gerade meinen Code bearbeitet und jetzt die App nicht crash.thanks.but ich bekomme nicht die Toast-Nachricht können Sie mir etwas vorschlagen. Warum wird der if-Block nicht ausgeführt und wie kann ich den Code bearbeiten, um ihn auszuführen? – M2skills
Sie geben keine Aktion für Ihre Absicht an, deshalb wird Ihr Toast nicht angezeigt. Sie könnten ein Extra in Ihre Absicht setzen, dies zu tun. Ich werde meine Antwort aktualisieren – 0xDEADC0DE