2017-12-19 17 views
-3

私はここで見つけるチュートリアルを仕事にしようとしています:https://developer.android.com/training/basics/firstapp/building-ui.htmlAndroidのチュートリアル「のシンボルを解決できません 『のsetText』」

アプリを構築しようとしたとき、私は上記のエラーを取得しています。私は答えを見つけようとしているのを見て回り、自分のコードをチェックしても無駄にしてみました。以下は

は私のコードです:

package com.example.chris.helloworld; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class DisplayMessageActivity extends AppCompatActivity { 

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

Intent intent = getIntent(); 
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

TextView textView = findViewById(R.id.textView); 
textView.setText(message); 
} 

とXML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.chris.helloworld.DisplayMessageActivity"> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="16dp" 
    android:text="TextView" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintHorizontal_bias="0.5" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 
</android.support.constraint.ConstraintLayout> 

任意の助けをいただければ幸いです。

+0

貧しい人々の質問に対する謝罪、私は今明らかな誤りを認識しています。 – Doragan

答えて

2

あなたはこれらの行を配置する必要があります。

のonCreate(またはその他の方法)インサイド
Intent intent = getIntent(); 
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

TextView textView = findViewById(R.id.textView); 
textView.setText(message); 

。そのようなコードをJavaのメソッドの外に置くことはできません。ビューをロードするので、私はonCreateと言います。それはいくつかまたは他の方法のonCreateから呼び出されるべきではなく、のonCreateから呼び出された外部メソッドも

を行うだろうだからあなたのコードは次のようになります。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_display_message); 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

    TextView textView = findViewById(R.id.textView); 
    textView.setText(message); 
} 

そして、これはすべてに適用されます。メソッドの外で行うことができることは、変数を宣言することだけです。これらの変数を設定するには、メソッド内で(テキストビューの場合はsetTextのようなメソッドを使用して)変数を変更する必要があります。

+1

答えをくれてありがとう、非常に有益な、私はこれを自分で見つけたはずだが! – Doragan

1

は、メソッド内でこれを持っていないのはなぜこの

public class DisplayMessageActivity extends AppCompatActivity { 

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

    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

    TextView textView = findViewById(R.id.textView); 
    textView.setText(message); 
} 

} 
1

を試すのonCreate?

Intent intent = getIntent(); 
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

TextView textView = findViewById(R.id.textView); 
textView.setText(message); 

これをonCreateなどの方法で入力する必要があります。

関連する問題