Harshit
Harshit

Reputation: 560

Application not launching in emulator

I am just trying my first android application. After going through a lot of videos and tutorials, I created my first app and its not launching on emulator.

Even I have seen various posts from stackoverflow but still cant find a solution for my problem.

Given below are my error messages from Debugger:-

1)Error Opening trace file no such file or directory

2)FATAL EXCEPTION:main

My Manifest File:-

     <?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.bijaru.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>
    <activity
        android:name="com.example.bijaru.SettingsActivity"
        android:label="@string/title_activity_settings" >
    </activity>
    <activity
        android:name="com.example.bijaru.LoggedinActivity"
        android:label="@string/title_activity_loggedin" >
    </activity>
</application>

And my main Activity File:-

   package com.example.bijaru;

 import java.util.ArrayList;
 import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
//import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
//import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {


Button login;
EditText username,password;
TextView status;
HttpPost httppost;
StringBuffer stringbuffer;
HttpResponse response;
HttpClient client;
List<NameValuePair> namevaluepair;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerOnButton();
}


private void addListenerOnButton() {
    // TODO Auto-generated method stub

    username=(EditText)findViewById(R.id.username);
    password=(EditText)findViewById(R.id.password);
    status=(EditText)findViewById(R.id.status);



    //final Context context = this;

    login = (Button) findViewById(R.id.login);

    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            switch (arg0.getId()){
            case R.id.login:
            login();
            break;
            }

          //  Intent intent = new Intent(context, LoggedinActivity.class);
                   //     startActivity(intent);   
        }

        private void login() {
            // TODO Auto-generated method stub
            try{

                client = new DefaultHttpClient();
                httppost=new HttpPost("hhtp://127.0.0.1/bijaru/check.php");
                namevaluepair = new ArrayList<NameValuePair>(1);
                namevaluepair.add(new BasicNameValuePair("username", username.getText().toString().trim()));
                namevaluepair.add(new BasicNameValuePair("password", password.getText().toString().trim()));
                httppost.setEntity(new UrlEncodedFormEntity(namevaluepair));

                response=client.execute(httppost);

                ResponseHandler<String> responsehandler =new BasicResponseHandler();
                final String response=client.execute(httppost,responsehandler);

                status.setText(""+response);

                if(response.equalsIgnoreCase("sucess"))
                {

                    startActivity(new Intent(MainActivity.this,LoggedinActivity.class));
                }

            }
            catch(Exception e)
            {
                e.printStackTrace();

            }

        }

    });


}


@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;
}

}

Error Logcat from Debuger:-

08-06 19:42:23.274: E/cutils-trace(1334): Error opening trace file: No such file or directory (2)
08-06 19:42:39.095: E/cutils-trace(1350): Error opening trace file: No such file or directory (2)
08-06 19:42:43.125: E/AndroidRuntime(1361): FATAL EXCEPTION: main
08-06 19:42:43.125: E/AndroidRuntime(1361): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bijaru/com.example.bijaru.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-06 19:42:43.125: E/AndroidRuntime(1361):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at android.os.Looper.loop(Looper.java:137)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at java.lang.reflect.Method.invoke(Method.java:525)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at dalvik.system.NativeStart.main(Native Method)
08-06 19:42:43.125: E/AndroidRuntime(1361): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-06 19:42:43.125: E/AndroidRuntime(1361):     at com.example.bijaru.MainActivity.addListenerOnButton(MainActivity.java:53)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at com.example.bijaru.MainActivity.onCreate(MainActivity.java:44)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at android.app.Activity.performCreate(Activity.java:5133)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-06 19:42:43.125: E/AndroidRuntime(1361):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-06 19:42:43.125: E/AndroidRuntime(1361):     ... 11 more

Upvotes: 0

Views: 580

Answers (2)

Sushil
Sushil

Reputation: 8488

In your layout, you have used TextView and in the code you are trying to typecast it to EditText whihc is causing Classcast error. In the code change EditText to TextView or in xml change TextView to EditText as per your need.

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

You have declared status as a TextView

TextView status;

but you try to initialize it as EditText

status=(EditText)findViewById(R.id.status);

You either need to change in your xml to TextView or in your java as EditText

Upvotes: 3

Related Questions