简体   繁体   中英

I Have a Android spinner or array adapter error but I'm not sure

I'm a newbie for android programmer.

I have error a spinner or adapter method or something else.I'm create a dropdown spinner and use array adapter method to call a integer array but some thing wrong and see This error.

07-17 13:46:42.496: E/AndroidRuntime(7501): FATAL EXCEPTION: main
07-17 13:46:42.496: E/AndroidRuntime(7501): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mpa.emvi/com.mpa.emvi.HomeActivity}: java.lang.InstantiationException: can't instantiate class com.mpa.emvi.HomeActivity
07-17 13:46:42.496: E/AndroidRuntime(7501):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at android.app.ActivityThread.access$700(ActivityThread.java:152)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at android.os.Looper.loop(Looper.java:137)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at android.app.ActivityThread.main(ActivityThread.java:5328)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at java.lang.reflect.Method.invokeNative(Native Method)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at java.lang.reflect.Method.invoke(Method.java:511)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at dalvik.system.NativeStart.main(Native Method)
07-17 13:46:42.496: E/AndroidRuntime(7501): Caused by: java.lang.InstantiationException: can't instantiate class com.mpa.emvi.HomeActivity
07-17 13:46:42.496: E/AndroidRuntime(7501):     at java.lang.Class.newInstanceImpl(Native Method)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at java.lang.Class.newInstance(Class.java:1319)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
07-17 13:46:42.496: E/AndroidRuntime(7501):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
07-17 13:46:42.496: E/AndroidRuntime(7501):     ... 11 more

and This is My class File

public abstract class HomeActivity extends FragmentActivity 
 implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener, OnItemSelectedListener {

    private CheckBox mTrafficCheckbox;

    private LocationManager mLocationManager;
    boolean statusOfGPS;
    private GoogleMap mMap;
    private LocationClient mLocationClient;
    private String DescribText = new String();

    private static final String TAG = "MyGPS_status";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);   

        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.layers_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        mTrafficCheckbox = (CheckBox) findViewById(R.id.checkBox1);

        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
        if (mMap != null) {
            updateTraffic();
            setUpLocationClientIfNeeded();
            mLocationClient.connect();
            mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE );
        }
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                 mMap.setMyLocationEnabled(true);
            }
        }
    }

    private void setUpLocationClientIfNeeded() {
        if (mLocationClient == null) {
          mLocationClient = new LocationClient(
              getApplicationContext(),
              this,  // ConnectionCallbacks
              this); // OnConnectionFailedListener
        }
     }

    public void sendEmail(View view){
        statusOfGPS = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        final Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","suphawut@mpatmail.com", null));

        if (mLocationClient != null && mLocationClient.isConnected() && statusOfGPS == true) {
            DescribText = "(For Test Emergency App)"+ 
                    "This is My Emergency Location on GPS " +
                    " <a href='https://maps.google.com/maps?q="+mLocationClient.getLastLocation().getLatitude()+","+mLocationClient.getLastLocation().getLongitude()+"&ll="+mLocationClient.getLastLocation().getLatitude()+","+mLocationClient.getLastLocation().getLongitude()+"&z=17'>click here</a>";

            i.putExtra(Intent.EXTRA_SUBJECT, "Emergency Location");
            i.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(DescribText));
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_LONG).show();
            }

         Log.d(TAG, "gps =" + statusOfGPS);
        }else{
            DescribText = "This is My Emergency Location on INTERNET" +
                    " <a href='https://maps.google.com/maps?q="+mLocationClient.getLastLocation().getLatitude()+","+mLocationClient.getLastLocation().getLongitude()+"&ll="+mLocationClient.getLastLocation().getLatitude()+","+mLocationClient.getLastLocation().getLongitude()+"&z=17'>click here</a>";
            i.putExtra(Intent.EXTRA_SUBJECT, "Emergency Location");
            i.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(DescribText));
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_LONG).show();
            }

          Log.d(TAG, "gps =" + statusOfGPS);                 
        }
    }


    @Override
    protected void onPause() {
        super.onPause();

    }

    /**
     * Callback called when disconnected from GCore. Implementation of {@link ConnectionCallbacks}.
     */
    @Override
    public void onDisconnected() {
      // Do nothing
    }

    /**
     * Implementation of {@link OnConnectionFailedListener}.
     */
    @Override
    public void onConnectionFailed(ConnectionResult result) {
      // Do nothing
    }

    @Override
    public void onLocationChanged(Location arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub

    }
    public void onProviderEnable(String provider){

    }
    public void onTrafficToggled(View view) {
        updateTraffic();
    }

    private void updateTraffic() {

        mMap.setTrafficEnabled(mTrafficCheckbox.isChecked());
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        setLayer((String) parent.getItemAtPosition(position));
    }

    private void setLayer(String layerName) {
        if (layerName.equals(getString(R.string.normal))) {
            mMap.setMapType(MAP_TYPE_NORMAL);
        } else if (layerName.equals(getString(R.string.hybrid))) {
            mMap.setMapType(MAP_TYPE_HYBRID);
        } else if (layerName.equals(getString(R.string.satellite))) {
            mMap.setMapType(MAP_TYPE_SATELLITE);
        } else if (layerName.equals(getString(R.string.terrain))) {
            mMap.setMapType(MAP_TYPE_TERRAIN);
        } else {
            Log.i("LDA", "Error setting layer with name " + layerName);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Do nothing.
    }
}

I'm not sure a problem may be a spinner,array adapter or something else but thank for any answer.

PS My English skill is poor.

Why you make the abstract class. Just remove the abstractness of the class.

Please check your Androidmanifest.xml, are you gave proper name there like .HomeActivity or (full package).HomeActivity, if not then make it proper and then clean your project.

And if not needed then remove Abstract label.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM