2016-08-04 130 views
4

我想通過學習教程和Android工作室Android應用建設。有關xml和導入的一些評論是有幫助的。我陷入了一個錯誤。我得到這個錯誤錯誤:(22,57)錯誤:無法找到符號變量activity_display_message在教程的Android出錯找不到符號變量activity_display_message

的誤差在進口方面我有固定與堆流量一些搜索。我失去了一些東西

DisplayMessageActivity

package com.example.rpinto.myfirstapp; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.ViewGroup; 
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 = new TextView(this); 
     textView.setTextSize(40); 
     textView.setText(message); 

     ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message); 
     layout.addView(textView); 
    } 
} 

activity_display_message.xml

<?xml version="1.0" encoding="utf-8"?> 
<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="com.example.rpinto.myfirstapp.DisplayMessageActivity" 
    tools:showIn="@layout/activity_display_message" 
    android:id="@+id/content"> 
</RelativeLayout> 

的build.gradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "24.0.1" 

    defaultConfig { 
     applicationId "com.example.rpinto.myfirstapp" 
     minSdkVersion 15 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.4.0' 
} 

回答

0

我已經通過清潔工程解決同樣的問題,然後生成gradle這個,請嘗試一次。

2

我想:

ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message); 

應該是:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.content); 
+0

不幸的是,將ViewGroup改爲RelativeLayout對我來說並不適用。 = \ – Tscott

0
"@layout/activity_display_message" 

這是一個佈局。不是身份證件。

你正確使用它這裏R.layout

setContentView(R.layout.activity_display_message); 

但是,這是錯誤的,因爲R.id

ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message); 
layout.addView(textView); 

校正方法最好應該是這樣的,但請注意,您可能希望在一些的LayoutParams TextView,否則它可能不會顯示在屏幕上。

@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 = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 

    ViewGroup layout = (ViewGroup) findViewById(R.id.content); 
    layout.addView(textView); 
} 
+0

非常感謝。就是這樣。有效。 –

4

不知道是否有人仍然需要這個問題的答案,但我想它是這樣的:

我在教程中看到這一點:

Note: The XML layout generated by previous versions of Android Studio might not include the android:id attribute. The call findViewById() will fail if the layout does not have the android:id attribute. If this is the case, open activity_display_message.xml and add the attribute android:id="@+id/activity_display_message" to the layout element.

所以,RES下 - > layout - > activity_display_message.xml,我輸入該行

android:id="@+id/activity_display_message" 

RelativeLayout標記內的任何位置。 paddingTop和工具:上下文字段

希望這有助於:d

+1

我也跟着教程,遇到同樣的問題,我仍然得到相同的錯誤消息:「錯誤:(18,60)錯誤:無法找到符號變量EXTRA_MESSAGE」 我已經everthing最新Andriod Studios和我的屬性ID已經存在於activity_display_message.xml文件中。 我有一種感覺,EXTRA_MESSAGE與Acivity_Main.java文件不能溝通,但我不確定它爲什麼看不到它。 – Tscott

1

下面是該更新的解決方案在我的情況而言,筆者隨機在兩者之間的機器人猛的。

意圖是主要活動的一個子類,所以沒有必要明確指定。這意味着我們可以稱之爲EXTRA_MESSAGE,而不是MainActivity.

EXTRA_MESSAGE

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_display_message); 

    Intent intent = getIntent(); 
    String message = intent.getStringExtra(EXTRA_MESSAGE); 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 

    ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message); 
    layout.addView(textView); 
+0

這解決了我的問題。從「MainActivity.EXTRA_MESSAGE」中爲字符串消息代碼行取出「MainActivity」對我來說是這樣的。 – Tscott

3

我有同樣的問題。在Android Studio中的說明「建設意向書」部分下,很容易錯過這一行:

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; 

確認這是包含在@覆蓋在上面的MainActivity.java和你所有的設置:)

+0

非常感謝。這在嘗試了很多答案後有所幫助 –

+0

你剛剛爲我節省了很多時間:) –

0

錯誤來自MainActivity.java文件。

TextView tv = (TextView) findViewById(R.id.editText); 

我在哪裏(R.id.editText); .....系統放(R.id.edit_message);

edit_message是添加到文本框中的函數,但它位於(EditText)「代碼塊」下。對於Java而言,不確定代碼塊的調用方式,也不確定程序爲什麼將這些代碼放入公共類中。希望這可以幫助。

package com.example.josh.myfirstapplication; 

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

public class MainActivity extends AppCompatActivity { 

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

    // Example of a call to a native method 
    TextView tv = (TextView) findViewById(R.id.editText); 
    tv.setText(stringFromJNI()); 
    } 

    /** 
    * A native method that is implemented by the 'native-lib' native library, 
    * which is packaged with this application. 
    */ 
    public native String stringFromJNI(); 

    // Used to load the 'native-lib' library on application startup. 
    static { 
     System.loadLibrary("native-lib"); 
    } 
} 
0

我有同樣的錯誤消息:找不到符號變量activity_display_message

我解決它通過添加機器人:ID在activity_display_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.galo.myapplication.DisplayMessageActivity" 
    android:id="@+id/activity_display_message"> 

</android.support.constraint.ConstraintLayout> 
0

檢查,如果你有這樣的代碼在MainActivity.java

public class MainActivity extends AppCompatActivity { 

    public static final String EXTRA_MESSAGE = "com.example.MyApplicationstart.MESSAGE"; 

「com.example.MyApplicationstart.MESSAGE」,其中「MyApplicationstart」是您項目的名稱。

-1

我也有同樣的問題。得到了糾正的錯誤,當我替換錯誤行: -

EditText editText = (EditText) findViewById(R.id.editText2); 
0

我有這個問題太多次試圖做這個教程,並添加到這裏唯一的答案,幫助我,這是刪除MainActivity.MainActivity.EXTRA_MESSAGE部分...

我也不得不導入DisplayMessageActivity.java如下:

import static android.provider.AlarmClock.EXTRA_MESSAGE;

這徹底解決了我所有的錯誤和允許我繼續教程。