我按照android官网的教程做了第一个android app,但是在我的nexus4手机上做测试时发现程序在完成第一个活动后无法进入第二个活动。想让大神们帮忙看一下,谢谢啦!package com.example.myfirstapp;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;
}
public void sendMessge(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);
}
}
package com.example.myfirstapp;import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;public class DisplayMessageActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView=new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
} /**
* 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);
}}
<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"
/></RelativeLayout><?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><?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="title_activity_display_message">My Message</string>
</resources>
android测试
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;
}
public void sendMessge(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);
}
}
package com.example.myfirstapp;import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;public class DisplayMessageActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView=new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
} /**
* 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);
}}
<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"
/></RelativeLayout><?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><?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="title_activity_display_message">My Message</string>
</resources>
android测试
解决方案 »
- 和新浪微博web一样自动填充邮箱后缀
- android端获取数据库,服务器上储存的图片怎么做
- 一个数字越界的问题,求指点
- Android4.0获取本机ip地址的问题
- 肿么办……关于activity
- Android开发,JS调后台java代码,创建videoView,页面没反应,请问这是为什么?
- 将原始xml内容显示在TextView中出错
- webview中的openFileChooser事件如何才能让弹出的菜单为中文??
- 如何设置application和activity的长宽为0
- ViewPager加载数据第一次不显示问题
- 将一个list传到下一个Activity
- JAVA怎样修改bmp图片的位深度,有可控制的参数吗?
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
没关系,报的什么错
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
没关系,报的什么错
控制面板显示的是这些,没有报错
[2013-10-11 01:48:45 - MyFirstApp] ------------------------------
[2013-10-11 01:48:45 - MyFirstApp] Android Launch!
[2013-10-11 01:48:45 - MyFirstApp] adb is running normally.
[2013-10-11 01:48:45 - MyFirstApp] Performing com.example.myfirstapp.MainActivity activity launch
[2013-10-11 01:48:45 - MyFirstApp] Automatic Target Mode: using device '047faa901cd02ed8'
[2013-10-11 01:48:45 - MyFirstApp] Uploading MyFirstApp.apk onto device '047faa901cd02ed8'
[2013-10-11 01:48:45 - MyFirstApp] Installing MyFirstApp.apk...
[2013-10-11 01:48:48 - MyFirstApp] Success!
[2013-10-11 01:48:48 - MyFirstApp] Starting activity com.example.myfirstapp.MainActivity on device 047faa901cd02ed8
[2013-10-11 01:48:48 - MyFirstApp] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.myfirstapp/.MainActivity }
[2013-10-11 02:59:25 - MyFirstApp] ------------------------------
[2013-10-11 02:59:25 - MyFirstApp] Android Launch!
[2013-10-11 02:59:25 - MyFirstApp] adb is running normally.
[2013-10-11 02:59:25 - MyFirstApp] Performing com.example.myfirstapp.MainActivity activity launch
[2013-10-11 02:59:25 - MyFirstApp] Automatic Target Mode: using device '047faa901cd02ed8'
[2013-10-11 02:59:25 - MyFirstApp] Uploading MyFirstApp.apk onto device '047faa901cd02ed8'
[2013-10-11 02:59:25 - MyFirstApp] Installing MyFirstApp.apk...
[2013-10-11 02:59:27 - MyFirstApp] Success!
[2013-10-11 02:59:27 - MyFirstApp] Starting activity com.example.myfirstapp.MainActivity on device 047faa901cd02ed8
[2013-10-11 02:59:28 - MyFirstApp] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.myfirstapp/.MainActivity }
发LogCat上来看看。