Giannis Thanasiou
Giannis Thanasiou

Reputation: 299

My app works only on emulator, not on the real device

I test my application on the emulator and it works fine. When I launch it on the real device, it doesn't work (components are load normally). It only consists of a textfield, where as soon as "1" is pressed on it, "Hello, world" should be written on it. I use:

import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
String current_string;
int length;
EditText et;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et = (EditText) findViewById(R.id.editText);

    et.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_1)) {
                et.setText("Hello, world!");
                current_string = et.getText().toString();
                length = current_string.length();
                et.setSelection(length);
                return true;
            }
            return false;
        }
    });
}

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

}

And the xml I use:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.testingname.testapp.app.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>

Upvotes: 0

Views: 308

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93561

Because on a real device you aren't going to see keycode events for anything except physical keys. If you had a hardware keyboard like the old blackberries it would use this API. Otherwise the data goes directly to the edit text, and you need to use a TextWatcher.

Upvotes: 0

Related Questions