java – fragment_ display_ Message: My First Android application error

This is my first time developing mobile applications

Environment: Eclipse Kepler, Java 1.7 backend:

I follow these steps:

1) Install the Android SDK, run the Android SDK manager, and install the required files

2) Installed ADT eclipse ADT plug-ins: https://dl-ssl.google.com/android/eclipse

At this stage of some standard documents, it says you must create an AVD now. The AVD icon is assumed to appear in my toolbar, but I can't find it anywhere

Now, I'm trying to develop my first application, as shown on this website: http://developer.android.com/training/basics/firstapp/index.html

I'm adding my code snippet as follows:

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,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 when pressing a button, I have fragments everywhere in my project directory_ main. XML and fragment_ display_ message. xml. Did you create these two files yourself? Or do we have to create them separately?

Have I also installed all the prerequisites for developing Android applications correctly? I'm not sure.

In addition, in displaymessactivity In Java, it represents: fragment_ display_ Message cannot be resolved or is not a field

So I can't run the file either

Please help. And long-term description of SRY

Solution

I just solved the same problem, and it still exists in the Android - Studio build your first application tutorial

In displaymessactivity In Java, I use r.layout activity_ display_ Message replaces r.layout fragment_ display_ message. This refers to activity_ display_ message. XML, otherwise it is redundant

You do not have to create a new fragment_ display_ message. xml.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>