0

シンプルなAndroidHelloWorld:

MainActivity.java:

package com.amaker.ch02.app;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    private TextView displayTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        displayTextView = (TextView)findViewById(R.id.DisplayTextView);
        displayTextView.setText("change in the code");    <--Right if delete the line
    }
}

res / layout / main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id = "@+id/DisplayTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

実行すると、Androidが表示されます:申し訳ありませんが、アプリケーションが予期せず停止しました。もう一度やり直してください。、しかし、私がコードを指しているのと同じように、コードMainActivity.javaを削除すればdisplayTextView.setText("change in the code");、すべてがOKです。

コード行の何が問題になっていますか?

4

1 に答える 1

0

あなたのコードは正しいようです。プロジェクトをクリーンアップしてビルドしてみてください。また、xmlとコードのtextview idを変更してみてください。nullになるDisplayTextView可能性があるため、この問題が発生します。コードをデバッグし、実際の原因を見つけます。またはlogcatの詳細を提供してください。

于 2012-02-01T03:37:20.907 に答える