2013-03-11 61 views
0

感谢您提供有用的建议,我编辑了最初的问题,因为我想展示项目的外观图像以及我得到的不同于教程指示应显示。我能够解决第一个问题(它只是有点离开),但是,在创建第一个意图后,我遇到了问题,如位于此处的文章的第二部分所述:developer.android.com/training/ basics/firstapp/starting-activity.html#receivetheintentAndroid开发人员第一次应用教程

项目构建,但我为项目设置的运行配置不显示下面代码中列出的任何输入元素。我不确定如何进一步解决问题,并真的很感激任何额外的帮助。再次感谢您的帮助。

模拟器: http://s1278.beta.photobucket.com/user/cetmrw791346/media/1_zps116f17a9.png.html

Package Explorer中: http://s1278.beta.photobucket.com/user/cetmrw791346/media/2_zps0f2b94a2.png.html

这里是相关文件:

**AndroidManifest.xml** 


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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.firstapp.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> 
     <activity 
      android:name="com.example.firstapp.DisplayMessageActivity" 
      android:label="@string/title_activity_display_message" 
      android:parentActivityName="com.example.firstapp.MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.firstapp.MainActivity" /> 
     </activity> 
    </application> 

</manifest> 


**MainActivity.java** 


package com.example.firstapp; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 

public class MainActivity extends Activity { 

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.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; 
    } 


    /** Called when the user clicks the Send button */ 
    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" 
    tools:context=".MainActivity" > 
    <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> 


**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="menu_settings">Settings</string> 
    <string name="title_activity_main">MainActivity</string> 
    <string name="title_activity_display_message">DisplayMessageActivity</string> 
    <string name="action_settings">Settings</string> 
    <string name="hello_world">Hello world!</string> 

</resources> 

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=".DisplayMessageActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 


**DisplayMessageActivity.java** 


package com.example.firstapp; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.support.v4.app.NavUtils; 
import android.annotation.TargetApi; 
import android.os.Build; 

public class DisplayMessageActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_display_message); 
     // Show the Up button in the action bar. 
     setupActionBar(); 
    } 

    /** 
    * Set up the {@link android.app.ActionBar}, if the API is available. 
    */ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    private void setupActionBar() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
     } 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      // This ID represents the Home or Up button. In the case of this 
      // activity, the Up button is shown. Use NavUtils to allow users 
      // to navigate up one level in the application structure. For 
      // more details, see the Navigation pattern on Android Design: 
      // 
      // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
      // 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 

回答

2

在控制台登录:

Error: No resource found that matches The given name (at 'title' with value '@string/action_settings')

平均s&需要定义里面res/values/strings.xml文件的action_settings字符串:

<resources> 
     <!-- Other String name- value --> 
    <string name="action_settings">Action Settings</string> 
</resources> 

和第二种方式是,如果你不使用Menu为您的应用则正好注释内res/menu/main.xmlandroid:title项目属性。

2

据抱怨menu/main.xml,不layout/activity_main.xml

看起来你的菜单项没有使用的android:title而不仅仅是title完全合格的名称,或者您还没有定义它使用的字符串。

相关问题