4

I am very new to Android development. I have tried to recreate an app where if you press a button, a message "new text string" is displayed. However, when I run the AVD and choose the Virtual Device, it doesn't display it. Just an old TestApp I had. Can anyone suggest a fix? I'd really appreciate it. My code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=      "http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:id="@+id/hello_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />


<Button android:id="@+id/trigger"
    android:layout_width="100dip" android:layout_height="100dip"
    android:text="@string/button1"
    android:layout_below="@+id/hello_text"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="111dp" />
 </RelativeLayout>

SimpleActivity.java

package com.example.simpleactivityexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import android.widget.*;
import android.view.View;
public class SimpleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
    Button startButton = (Button) findViewById(R.id.trigger);
    startButton.setOnClickListener(new View.OnClickListener()
    {
        private TextView tv = (TextView) findViewById(R.id.hello_text);
        public void onClick(View view)
        {
            tv.setText("new text string");
        }
    }
    );

}
@Override
protected void onStart() {
    super.onStart();
    Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
    super.onResume();
    Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
    super.onRestart();
    Toast.makeText(this, "onRestart", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
    Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
    super.onPause();
}
@Override
protected void onStop() {
    Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show();
    super.onStop();
}
@Override
protected void onDestroy() {
    Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
    super.onDestroy();
}


}

Manifest

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

<application
    android:debuggable="true"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.simpleactivityexample.SimpleActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  </application>

 </manifest>

Strings.xml

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
 <string name="app_name">Dietary Application</string>
 <string name="hello">Hello!</string>
 <string name="button1">This is a button!</string>
 </resources>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
user3388470
  • 91
  • 1
  • 2
  • 7
  • I'm really sorry about the bad layout, I'm completely new to this. – user3388470 Mar 06 '14 at 14:05
  • Since the problem is not in your code you should probably post a console output instead. – Yaroslav Mytkalyk Mar 06 '14 at 14:09
  • C:\Android\sdk\tools\emulator.exe -avd AVD_for_Nexus_S_by_Google -netspeed full -netdelay none Device connected: emulator-5554 Device online: emulator-5554 Target device: AVD_for_Nexus_S_by_GoogleUploading local path: C:\Androd\AndroidStudioProjects\SimpleActivityExample\SimpleActivityExample\build\apk\SimpleActivityExample-debug-unaligned.apk remote path: /data/local/tmp/com.example.simpleactivityexample Installing com.example.simpleactivityexample DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.simpleactivityexample" Error:Could not access Package Manager.Is system running? – user3388470 Mar 06 '14 at 14:28

3 Answers3

3

I managed to solve it.I am using Android Studio by the way. I went to the run tab at the top of the screen and went into ''Edit Configurations''. I then ticked ''Show chooser dialog''. I started the AVD and when it was fully started, A pressed the run function(green triangle) and it started on the Android. Thanks so much to everyone who helped!

user3388470
  • 91
  • 1
  • 2
  • 7
0

Your code seems fine; check the logcat and console outputs when you click 'Run as Android Application' to see if the install is working.

Logcat is located in the top menu under

Window- Show View - Other - Android - Logcat

Also try cleaning the project and restarting the emulator; and also, if the app doesn't show up when the emulator loads, try, without exiting the emulator, to run it again.

Mesher
  • 46
  • 4
  • I use Android Studio, I can't find the logcat. – user3388470 Mar 06 '14 at 14:35
  • @user3388470 Your emulator may not be online when you attempt to install; try waiting for the emulator to start completely, then choosing the emulated device from the menu and then installing (having upgraded your AS first). – Mesher Mar 06 '14 at 16:06
0

Are you using the latest studio? Seems like an old bug

Error when running emulator in Android Studio

Try waiting until the emulator boots, and then run the project.

Community
  • 1
  • 1
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99