HAWK
HAWK

Reputation: 53

Android: facing Errorjava.lang.ClassCastException

What I am trying to achieve: Trying to send data from my app's fields to my Servlet on WebAppServer.

I am facing ClassCastException issue, I tried implementing the suggestions I found on StackOverflow in relevant discussion and made some corrections like, Intializing my objects in 'onCreate()' etc.

Note: I am using Volley Library of Android for sending my request to servlet.

I tried most of the changes in my .MainActivity and Manifest file, but I could not get rid of this error. Can you please advice.

My 'MainActivity.java' File

package com.example.hawk;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener {

    EditText var_sp = null;
    EditText var_ect = null;
    EditText var_er = null;
    EditText var_iat = null;
    EditText var_atp = null;
    Button submit;
    RequestQueue rq = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        var_speed = (EditText) findViewById(R.id.sp);
        var_eng_coolant_temp = (EditText) findViewById(R.id.ect);
        var_eng_rpm = (EditText) findViewById(R.id.er);
        var_in_air_temp = (EditText) findViewById(R.id.iat);
        var_abs_throttle_postn = (EditText) findViewById(R.id.atp);
        submit = (Button) findViewById(R.id.submit);        
        rq=Volley.newRequestQueue(this);
        submit.setOnClickListener(this);
    }
@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();
    if (id == R.id.action_settings) {
    return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.submit:

    new Thread(new Runnable() {
    public void run() {
    try{
        StringRequest postReq = new StringRequest(Request.Method.POST, "http://192.168.0.103:8080/OBD_Feeder/post",new Response.Listener<String>() {
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("sp",var_speed.getText().toString());
                params.put("ect",var_eng_coolant_temp.getText().toString());
                params.put("er",var_eng_rpm.getText().toString());
                params.put("iat",var_in_air_temp.getText().toString());
                params.put("atp",var_abs_throttle_postn.getText().toString());
                return params;
            }

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub

            }

        }, null);

    rq.add(postReq);
    }catch(Exception e)
    {
        e.printStackTrace();
    }

    }
    }).start();

    break;


    }
    }
}

My Manifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hawk"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:name=".MainActivity"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>       
    </application>

</manifest>

My Logcat is as follow

> 08-15 00:11:50.807: W/dalvikvm(9272): threadid=1: thread exiting with
> uncaught exception (group=0x41804ba8) 08-15 00:11:50.817:
> E/AndroidRuntime(9272): FATAL EXCEPTION: main 08-15 00:11:50.817:
> E/AndroidRuntime(9272): Process: com.example.hawk, PID: 9272 08-15
> 00:11:50.817: E/AndroidRuntime(9272): java.lang.RuntimeException:
> Unable to instantiate application com.example.hawk.MainActivity:
> java.lang.ClassCastException: com.example.hawk.MainActivity cannot be
> cast to android.app.Application 08-15 00:11:50.817:
> E/AndroidRuntime(9272):   at
> android.app.LoadedApk.makeApplication(LoadedApk.java:507) 08-15
> 00:11:50.817: E/AndroidRuntime(9272):     at
> android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301)
> 08-15 00:11:50.817: E/AndroidRuntime(9272):   at
> android.app.ActivityThread.access$1500(ActivityThread.java:135) 08-15
> 00:11:50.817: E/AndroidRuntime(9272):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
> 08-15 00:11:50.817: E/AndroidRuntime(9272):   at
> android.os.Handler.dispatchMessage(Handler.java:102) 08-15
> 00:11:50.817: E/AndroidRuntime(9272):     at
> android.os.Looper.loop(Looper.java:136) 08-15 00:11:50.817:
> E/AndroidRuntime(9272):   at
> android.app.ActivityThread.main(ActivityThread.java:5001) 08-15
> 00:11:50.817: E/AndroidRuntime(9272):     at
> java.lang.reflect.Method.invokeNative(Native Method) 08-15
> 00:11:50.817: E/AndroidRuntime(9272):     at
> java.lang.reflect.Method.invoke(Method.java:515) 08-15 00:11:50.817:
> E/AndroidRuntime(9272):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
> 08-15 00:11:50.817: E/AndroidRuntime(9272):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 08-15
> 00:11:50.817: E/AndroidRuntime(9272):     at
> dalvik.system.NativeStart.main(Native Method) 08-15 00:11:50.817:
> E/AndroidRuntime(9272): Caused by: java.lang.ClassCastException:
> com.example.hawk.MainActivity cannot be cast to
> android.app.Application 08-15 00:11:50.817: E/AndroidRuntime(9272):
>   at
> android.app.Instrumentation.newApplication(Instrumentation.java:990)
> 08-15 00:11:50.817: E/AndroidRuntime(9272):   at
> android.app.Instrumentation.newApplication(Instrumentation.java:975)
> 08-15 00:11:50.817: E/AndroidRuntime(9272):   at
> android.app.LoadedApk.makeApplication(LoadedApk.java:502) 08-15
> 00:11:50.817: E/AndroidRuntime(9272):     ... 11 more

Upvotes: 2

Views: 337

Answers (1)

nhaarman
nhaarman

Reputation: 100448

Here:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".MainActivity"
    android:theme="@style/AppTheme" >

Remove the android:name=".MainActivity line.

That name property specifies what Application class to use. Since MainActivity does not extend Application, your app crashes.

You have actually already defined which Activity is the main Activity by declaring an intent-filter with action android.intent.action.MAIN.

Upvotes: 2

Related Questions