3

I am developing a Mobile app for the first time

Environment : Eclipse Kepler, Java 1.7 Backend :

I have followed the following steps :

1) Installed Android SDK and run the Android SDK Manager, and installed the required files

2) Installed ADT Eclipse ADT Plugin : https://dl-ssl.google.com/android/eclipse

At this stage in some standard docs it said that you have to now create an AVD, the AVD icon was suppose to appear in my toolbar, but I couldnt find it anywhere.

Now, I am trying to develop my first app as shown on this site : http://developer.android.com/training/basics/firstapp/index.html

I am jus adding my code snippets as below for reference :

main.xml :

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

strings.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="uploading">Uploading…</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_display_message">DisplayMessageActivity</string>
</resources>

AndroidManifest.xml :

<!--  <?xml version="1.0" encoding="utf-8" standalone="no"?> -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amazon.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="16" 
    android:targetSdkVersion ="20" />
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:allowBackup="true" >      
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
    </activity>
    <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.amazon.demo.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.amazon.demo.MainActivity" />
    </activity>        
</application>    
</manifest>

MainActivity.java

public class MainActivity extends ActionBarActivity {   
public final static String EXTRA_MESSAGE = "com.amazon.demo.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
@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);
}   
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);
}   
}

activity_main.xml :

<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" 
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.amazon.demo.MainActivity" >
 <EditText android:id="@+id/edit_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    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 :

public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_display_message);
    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);
    // Set the text view as the activity layout
    setContentView(textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_message, 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);
}   
public static class PlaceholderFragment extends Fragment {
    public PlaceholderFragment() { }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_display_message,container, false);
          return rootView;
   }
}
}

activity_display_message.xml :

<RelativeLayout 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: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.amazon.demo.DisplayMessageActivity" >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
</RelativeLayout>

Now unlike the My First App crashes when pressing a button I dnt have the fragment_main.xml and the fragment_display_message.xml anywhere in my project dir. Are these two files created on their own? Or do we have to separately create them?

Also have I installed all the prerequisites for developing the Android application correctly? I am not sure.

Also, in the DisplayMessageActivity.java it says : fragment_display_message cannot be resolved or is not a field

Hence I am not able to run the file either.

Please help. and sry for the long description.

Community
  • 1
  • 1
user3868051
  • 1,147
  • 2
  • 22
  • 43
  • for fragment_display_message it is showing 2 fixes : either create a field or a constant named fragment_display_message in layout – user3868051 Aug 14 '14 at 11:18
  • similarly, for edit_message, it is again showing 2 fixes :either create a field or a constant in type id – user3868051 Aug 14 '14 at 11:18

4 Answers4

15

I've just solved the same problem, which still exists in the ANDROID-STUDIO Building your first app tutorial.

In DisplayMessageActivity.java, I replaced R.layout.fragment_display_message with R.layout.activity_display_message. This references activity_display_message.xml which is otherwise redundant.

You don't have to create a new fragment_display_message.xml.

user1501247
  • 326
  • 3
  • 13
2

When I was creating a fragment using a blank activity I found the option Fragment Layout Name missing as an option, if this was the case for you, you'll need to create fragment_display_message.xml for yourself as well as a few other xml files. To check the files and the code that you need you could refer to this link. This contains all the files which are needed for the fragment.

Hope this has helped.

Community
  • 1
  • 1
James
  • 338
  • 1
  • 2
  • 16
  • hey thanks for the prompt reply... yes as per what you sid i ticked the option of fragment layout name, it allowed me to create the the fragment file for MainActivity but its not allowing me to create another activity of type "Blank activity with fragment layout name", it says min version required is 11, but where to make those changes now? – user3868051 Aug 14 '14 at 09:43
  • @user3868051 You say you don't have `fragment_main.xml` and `fragment_display_message.xml` anywhere in your project directory, these two files, if not already created, need to be created by you and should be created in the directory `myfirstapp/res/layout` – James Aug 14 '14 at 09:46
  • This template requires a minimum SDK version of at least 7, and the current min version is 1 – user3868051 Aug 14 '14 at 09:46
  • the above comment is what i get when i try to set the hierarchical parent as MainActivity – user3868051 Aug 14 '14 at 09:47
  • 1
    You can change the min version of sdk in the file AandroidManifest.xml` there should be a piece of code which reads ` ` change the `android:minSdkVersion` to 11. – James Aug 14 '14 at 09:48
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59342/discussion-between-james-and-user3868051). – James Aug 14 '14 at 09:54
1

In your AndroidManifest.xml, need some of the tag. Please reference my sample AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bm.app.shiftmanagement"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <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>

In your AndroidManifest.xml, need to modify this place.

<activity
    android:name=".MainActivity"
    android:label="@string/title_activity_main" >
    <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
B M
  • 1,863
  • 1
  • 25
  • 40
  • actually the log file is empty, when i click run as "android application" it says "your project contains errors, cannot run" – user3868051 Aug 14 '14 at 11:14
  • I think you need to learn step by step android tutorial. Links for you. http://www.vogella.com/tutorials/android.html another one is my reference link http://androiduiux.com/2012/06/27/new-to-android-designdevelopment-heres-a-list-of-great-resources-and-references-available/ – B M Aug 15 '14 at 01:41
1

I had a same issue. There is no need to create fragment_main.xml and fragment_display_message.xml. Just update the file /MyFirstApp/src/com/example/myfirstapp/DisplayMessageActivity.java. Replace the line:

View rootView = inflater.inflate(R.layout.fragment_display_message,container, false);

with:

View rootView = inflater.inflate(1,container, false);

The minimum SDK version should be 11 in the file /MyFirstApp/AndroidManifest.xml:

android:minSdkVersion="11"

And you can continue.

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54