7 Replies - 4258 Views - Last Post: 28 December 2016 - 11:24 PM

#1 trickstar34   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 109
  • Joined: 05-June 09

Android Studio Beginner App not Working

Posted 21 December 2016 - 11:15 PM

Ok guys, so I'm getting irritated cause Android Studio has been giving me trouble from the get go. First off it literally took 2 hours to install, 6 times the time for the download. So it started out not letting me see previews because apparently it didnt install NDK and some other library by default and after awhile of Google searching fixed that. So after I finally got that all done I thought, great I'm good to go and started doing the provided First App Tutorial and followed every step to a T yet I'm getting this error:

Attached Image

Why am I getting this error and how do I fix it? I find really frustrating that following step by step a tutorial by the actual developer of the IDE is producing errors.

Here is my MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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


    /* When send button is clicked  */

    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);

    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */

    public native String stringFromJNI();

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onclick="sendMessage" />
</LinearLayout>


DisplayMessageActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

public class DisplayMessageActivity extends AppCompatActivity {


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

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.class.EXTRA_MESSAGE);
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
        layout.addView(textView);
    }
}


activity_display_message.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_display_message"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ledvinatechnologies.www.cashtracker.DisplayMessageActivity">

</RelativeLayout>


strings.xml

<resources>
    <string name="app_name">First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
</resources>

This post has been edited by trickstar34: 21 December 2016 - 11:19 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Android Studio Beginner App not Working

#2 wtp   User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 338
  • Joined: 08-December 11

Re: Android Studio Beginner App not Working

Posted 23 December 2016 - 09:14 AM

That's android for you. Between downloading the IDE, SDK, build tools, creating a virtual device, and then downloading a different SDK and build tools needed for your particular project, it can easily take a couple hours. Just remember this for next time and try to start your IDE and virtual device a while before you start coding.

You're getting the extra error message because EXTRA_MESSAGE isn't defined. EXTRA_MESSAGE should be a string. EXTRA_MESSAGE is the key and "message" is the value. You're calling another activity with the intent and passing it a message. EXTRA_MESSAGE is the key you use to get the message in the activity you're calling. So just make EXTRA_MESSAGE a string with whatever value.
intent.putExtra(EXTRA_MESSAGE, message);
Notice how the activity you're calling is getting the message.
intent.getStringExtra(MainActivity.class.EXTRA_MESSAGE)
putextra

Your other error says it can't fine resolve TextView in mainactivity. Although I don't see you reference TextView at all in MainActivity. Maybe try adding import android.widget.TextView; anyway or rebuilding your project.

+1 for including your error and code.

This post has been edited by wtp: 23 December 2016 - 09:18 AM

Was This Post Helpful? 1
  • +
  • -

#3 trickstar34   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 109
  • Joined: 05-June 09

Re: Android Studio Beginner App not Working

Posted 28 December 2016 - 10:37 PM

So I just added "String EXTRA_MESSAGE = new String();" to MainActivity.java and it stopped the error on that one but in DisplayMessageActivity.java EXTRA_MESSAGE still shows up as red on the following line and when I put my mouse over it it says "cannot resolve symbol"

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);


What am I supposed to do? I find very frustrating they make errors in their own tutorial like this..

Attached Image

This post has been edited by trickstar34: 28 December 2016 - 10:48 PM

Was This Post Helpful? 0
  • +
  • -

#4 wtp   User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 338
  • Joined: 08-December 11

Re: Android Studio Beginner App not Working

Posted 28 December 2016 - 10:48 PM

Make it static. string static extra...
Was This Post Helpful? 0
  • +
  • -

#5 trickstar34   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 109
  • Joined: 05-June 09

Re: Android Studio Beginner App not Working

Posted 28 December 2016 - 10:51 PM

View Postwtp, on 28 December 2016 - 10:48 PM, said:

Make it static. string static extra...


What do you mean make it static? Like in MainActivity? Man when I do Java in Eclipse for school and transfer data between objects its a lot simpler than this.
Was This Post Helpful? 0
  • +
  • -

#6 trickstar34   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 109
  • Joined: 05-June 09

Re: Android Studio Beginner App not Working

Posted 28 December 2016 - 10:59 PM

Could you show me a corrected version of where you are talking about in the code? The code I've showed you is literally what the tutorial gave line for line so its the tutorial giving me bad information. When I want to get data from another object in a standard Java application I just write a object.get(dataID) method and it works fine without having to make anything static.
Was This Post Helpful? 0
  • +
  • -

#7 wtp   User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 338
  • Joined: 08-December 11

Re: Android Studio Beginner App not Working

Posted 28 December 2016 - 11:15 PM

String EXTRA_MESSAGE = new String();
Make that static

You're on the right track. You have to make it static because you can't access a classes variable without an instance of that class unless it's static which means it's the same value for any instance. Just look up static and the error messages when you come across them.

This post has been edited by wtp: 28 December 2016 - 11:18 PM

Was This Post Helpful? 1
  • +
  • -

#8 trickstar34   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 109
  • Joined: 05-June 09

Re: Android Studio Beginner App not Working

Posted 28 December 2016 - 11:24 PM

I get what you mean, its working now, I declared it inside the sendMessage() method thats why I was confused as to why I'd make it static. I put it as a field variable in MainActivity and set it as static and it worked. Thanks for putting me in the right direction.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1