Fragment_display_message: My first android app error
I am developing a mobile app for the first time
Environment: Eclipse Kepler, Java 1.7 Backend:
I followed these steps:
1) Installed Android SDK and launched Android SDK Manager and installed required files
2) Installed ADT plugin Eclipse ADT: https://dl-ssl.google.com/android/eclipse
At this point in some standard docs, he said that now you have to create an AVD, the AVD icon should appear on my toolbar, but I couldn't find it anywhere.
Now I am trying to develop my first app as shown in this site: http://developer.android.com/training/basics/firstapp/index.html
I am adding my code snippets as shown below:
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 My First App crashes on button click , I do not have fragment_main.xml and fragment_display_message.xml files anywhere in my project. Are these two files self-generated? Or should we create them separately?
Also have I set all the prerequisites for developing an Android app correctly? I'm not sure.
Also, DisplayMessageActivity.java says: fragment_display_message cannot be resolved or is not a field
Hence, I cannot run the file.
Please, help. and I apologize for the long description.
source to share
I just solved the same problem still exists in ANDROID-STUDIO . Create your first app tutorial.
In DisplayMessageActivity.java, I replaced R.layout.fragment_display_message
with R.layout.activity_display_message
. This indicates activity_display_message.xml
that it is otherwise redundant.
You don't need to create a new one fragment_display_message.xml
.
source to share
When I was creating the snippet with blank activity
, I found the option was Fragment Layout Name
missing as an option, if that was the case, you need to create fragment_display_message.xml
for yourself as well as several other xml
files. To check the files and code you need, you can refer to this link. This contains all the files needed for the snippet.
Hope this helped.
source to share
In your AndroidManifest.xml, you will need the tag part. Please refer to my AndroidManifest.xml sample.
<?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 this location needs to be changed.
<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>
source to share
I had the same problem. 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);
from:
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.
source to share