Android服务错误尝试调用虚拟方法

时间:2015-11-27 09:27:45

标签: android android-service android-adapter android-service-binding

我正在创建一个应用程序,其中java中的服务器在其他活动中发送Place数组和其他内容。所以我创建了一个服务,从不同的活动连接到服务器。我还没有实现服务器连接,但我正在模拟服务器响应,我将其作为参数传递给适配器(适配器已经过测试),我创建这个地址以在ListView中显示这些位置。所以我在我的活动中得到了我的Place数组,并将它从服务中的方法设置为返回对象,但是当我运行应用程序时它崩溃并且我收到了此错误消息:

  

java.lang.NullPointerException:尝试调用虚方法   ' com.remedialguns.smartourist.Place []   com.remedialguns.smartourist.ConnectionService.getPlaces()'在null   对象

UPDATE现在是同样的错误,但是在name.setText的myAdapter类中... 所以这是我的服务。

package com.remedialguns.smartourist;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Locale;

public class ConnectionService extends Service {

    Socket s;
    PrintStream os;

    private static final String TAG="com.remedialguns.smartourist";
    private final IBinder myBinder = new LocalBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    public void sendProfileData(){
        //send profile data to server
    }

    public Place[] getPlaces(){
        Place[] PlacesToShow = new Place[10];

        //F̶a̶k̶e̶ ̶d̶a̶t̶a̶ Test data
        PlacesToShow[0]= new Place("MUSEO","Museo Nacional Agropecuario", 0.15, 0.4, 0.12);
        PlacesToShow[1]= new Place("MUSEO","Museo Arqueológico Junín",0.10, 0.78, 0.44);
        PlacesToShow[2]= new Place("MUSEO","Museo Botero", 0.2, 0.8, 0.08);
        PlacesToShow[3]= new Place("MUSEO","Museo de Zea", 0.3, 0.65, 0.23);
        PlacesToShow[4]= new Place("MUSEO","MUSEO DEL ORO", 0.13, 0.56, 0.12);
        PlacesToShow[5]= new Place("MUSEO","MUSEO DE ARTE COLONIAL", 0.3, 0.67, 0.14);
        PlacesToShow[6]= new Place("MUSEO","MUSEO HISTORICO DE LA POLICIA NACIONAL", 0.34, 0.3, 0.33);
        PlacesToShow[8]= new Place("MUSEO","MUSEO DE LOS NIÑOS", 0.05, 0.65, 0.03);
        PlacesToShow[7]= new Place("MUSEO","Museo Nacional", 0.15, 0.4, 0.12);
        PlacesToShow[9]= new Place("MUSEO","MUSEO MILITAR", 0.07, 0.5, 0.6);
        return PlacesToShow;
    }



    public class LocalBinder extends Binder {
        ConnectionService getService(){
            return ConnectionService.this;
        }
    }
}

这是我使用ListView和适配器的活动。(更新)

package com.remedialguns.smartourist;

import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.os.IBinder;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import com.remedialguns.smartourist.ConnectionService.LocalBinder;

import com.google.android.gms.location.places.Places;

public class ListActivity extends AppCompatActivity {

    ConnectionService tcpService;
    boolean isBound=false;
    //Place[] PlacesToShow=tcpService.getPlaces();
    Place[] PlacesToShow=new Place[10];



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Intent i = new Intent(this, ConnectionService.class);
        bindService(i, myConnection, Context.BIND_AUTO_CREATE);
//        PlacesToShow=tcpService.getPlaces();


        if (isBound) {
            PlacesToShow = tcpService.getPlaces();
        }

        //Place[] PlacesToShow=tcpService.getPlaces();

        ListAdapter MyAdapter = new MyAdapter(this, PlacesToShow);
        ListView ListPlaces=(ListView) findViewById(R.id.MyList);
        ListPlaces.setAdapter(MyAdapter);

        ListPlaces.setOnItemClickListener(
                new AdapterView.OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        Context context = view.getContext();
                        TextView textViewItem = ((TextView)view.findViewById(R.id.name));
                        String name =textViewItem.getText().toString();
                        TextView textViewItem2 = (TextView)findViewById(R.id.description);
                        String descripcion=textViewItem2.getText().toString();
                        Toast.makeText(context,"lugar: "+name+", descripcion: "+descripcion,Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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);
    }

    private ServiceConnection myConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            tcpService = binder.getService();
            PlacesToShow = tcpService.getPlaces();
            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBound=false;
        }
    };


}

这是我的activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_list"
        tools:context="com.remedialguns.smartourist.ListActivity">

    <!--    <TextView
            android:id="@+id/answer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"


    android:text="these are the places that our smart monkeys had find for you."/>
-->
    <ListView
        android:id="@+id/MyList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.remedialguns.smartourist" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="android.permission.USE_CREDENTIALS" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
        </activity>
        <activity
            android:name=".RealMainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
        </activity>
        <activity
            android:name=".ListActivity"
            android:label="@string/app_name"
            android:parentActivityName=".RealMainActivity"
            android:theme="@style/AppTheme.NoActionBar" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.remedialguns.smartourist.RealMainActivity" />
        </activity>

        <service
            android:name=".ConnectionService"
            android:enabled="true"
            android:exported="true" >
        </service>
    </application>

</manifest>

这是我的适配器类

package com.remedialguns.smartourist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends ArrayAdapter<Place> {

    MyAdapter(Context context, Place[] places){
        super(context, R.layout.visual_place, places);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater MyInflater = LayoutInflater.from(getContext());
        View placeView = MyInflater.inflate(R.layout.visual_place, parent, false);

        Place singlePlaceItem=getItem(position);
        TextView name=(TextView) placeView.findViewById(R.id.name);
        ImageView icon =(ImageView) placeView.findViewById(R.id.icon);
        TextView description = (TextView) placeView.findViewById(R.id.description);

        name.setText(singlePlaceItem.getName().toString());
        icon.setImageResource(R.mipmap.ic_launcher);
        description.setText("Cost "+singlePlaceItem.getCost()+", distance "+singlePlaceItem.getDistance()+", rate "+singlePlaceItem.getRate()+" *");
        return placeView;

    }
}

请帮帮我。

1 个答案:

答案 0 :(得分:0)

下面:

bindService(i, myConnection, Context.BIND_AUTO_CREATE); 
Place[] PlacesToShow=new Place[10];
PlacesToShow=tcpService.getPlaces();

tcpService null可能使用的是在调用getPlaces()方法之前调用onServiceConnected方法。

在使用isBound对象之前,使用onServiceConnected检查tcpService是否已被调用:

if(isBound){
  // 
  PlacesToShow=tcpService.getPlaces();
} 

并使用onServiceConnected方法:

@Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            tcpService = binder.getService();
            PlacesToShow=tcpService.getPlaces();
            isBound=true;
}

另外,请确保在ConnectionService中添加AndroidManifest.xml作为服务。