gpt4 book ai didi

Android开发人员培训基础知识,文本不会显示

转载 作者:行者123 更新时间:2023-11-29 21:25:19 24 4
gpt4 key购买 nike

我刚开始从 developer.android.com/training/basics 学习 android 应用程序开发,我已经按照说明构建了应用程序,但是当我单击“发送”按钮时,文本不会显示,并且出现错误:不幸的是,FirstApp 已停止工作。

以下是 DisplayMessageActivity 类:

package com.example.firstapp;

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.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
//get the message from intent
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

//create the text view
TextView textView = (TextView) findViewById(R.id.m);
textView.setTextSize(40);
textView.setText(message);



super.onCreate(savedInstanceState);

//set the text view as activity layout
setContentView(textView);

// 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 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) {
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);
}

下面是 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:id="@+id/m"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>

之前activity_display_message中的TextView是这样的:

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

所以在我按下发送按钮后,输出总是 Hello World!不管我输入什么。

最佳答案

setContentView() 之前,findViewById() 方法返回 null
像这样更改您的 onCreate() 方法...

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);

//create the text view
TextView textView = (TextView) findViewById(R.id.m);
textView.setTextSize(40);

//get the message from intent
Intent intent=getIntent();
if(intent != null) {
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
if(message != null) {
textView.setText(message);
}
}
// Show the Up button in the action bar.
setupActionBar();
}

关于Android开发人员培训基础知识,文本不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20300084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com