Zishan
Zishan

Reputation: 623

No output of my app in emulator

I am quite new in android , so I am in problem , I want to create a textbox , my code is below

main_xml file :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".MainActivity" >

         <EditText
               android:id="@+id/mytextText"
               android:layout_width="match_parent"
               android:layout_height="wrap_content" >
               <requestFocus />
         </EditText>
</LinearLayout>     

Java Code is :

 package com.example.text;
 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;

 import android.app.Activity;

 import android.os.Bundle;

 import android.view.KeyEvent;

 import android.view.View;

 import android.view.View.OnKeyListener;

 import android.widget.EditText;

 import android.widget.Toast;

 public class MainActivity extends Activity {

       private EditText edittext;

       @Override
       public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);

             setContentView(R.layout.activity_main);

             addKeyListener();
       }

       public void addKeyListener() {
            // get edittext component
            edittext = (EditText) findViewById(R.id.menu_settings);

           // add a keylistener to monitor the keaybord avitvity...
           edittext.setOnKeyListener(new OnKeyListener() {
                    public boolean onKey(View v, int keyCode, KeyEvent event) {                                                                  

                           // if the users pressed a button and that button was "0"
                           if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_0)) { 
                                // display the input text....
                                Toast.makeText(MainActivity.this,edittext.getText(), Toast.LENGTH_LONG).show();
                                return true;

                                // if the users pressed a button and that button was "9"

                           } else if ((event.getAction() == KeyEvent.ACTION_DOWN)  && (keyCode == KeyEvent.KEYCODE_9))   {
                               // display message
                               Toast.makeText(MainActivity.this, "Number 9 is pressed!", Toast.LENGTH_LONG).show();
                               return true;
                           }

                           return false;

                    }
          });

     }
 }

My Output I in ecilips is that:

But no apps is creatd in my emulator, I don't understand ,no error in code, apk is lanching, but no ouptut plz, help me

Upvotes: 0

Views: 536

Answers (3)

Hari Haran
Hari Haran

Reputation: 1553

Add your MainActivity class in android manifest file then chenge the edit text id reference it should be same in xml and java code then only in perform some action.suppose in your xml edit text id mytext it should be same in java class. now run the program it should be launch.

Upvotes: 0

GrIsHu
GrIsHu

Reputation: 23638

It may because you might have not declared your activity as LAUNCHER in your manifest. That is why its just installed not launched.

Declare your activity as launcher activity in your manifest as below:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
   >
    <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>

<intent-filter> defines the types of intents that an activity, service, or broadcast receiver can respond to. An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle.

Also in your activity you have wrongly defined the id of your EditText which differs in your layout file.

Change the id of your EditText R.id.menu_settings to R.id.mytextText. Initialize EditText as below:

 edittext = (EditText) findViewById(R.id.mytextText);

Upvotes: 1

Abin
Abin

Reputation: 550

edittext = (EditText) findViewById(R.id.menu_settings);

change to

edittext = (EditText) findViewById(R.id.mytextText);

will work most probably

replay if not working

Upvotes: 0

Related Questions