2016-07-26 28 views
0

我在android studio中訓練我的java技能。 在我的Java HomeActivity我嘗試使用XML文件在我的應用程序使用setContentView xml文件錯誤

setContentView(R.layout.home_layout) 

但是Android工作室所有的時間顯示了我的錯誤。我很乾淨,重建我的應用程序並重置android studio,但仍然有錯誤。我沒有任何想法,我一直在尋找在這個論壇上的任何解決方案,但仍無法工作的正確

這是HomeActivity.java

的完整代碼
package my.domain.appname; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 

import android.R; 

public class HomeScreen extends AppCompatActivity { 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); AppCompatActivity 

     setContentView(R.layout.home_layout); 
    } 

    public void OnNextClick(View view) { 
     Intent intent = new Intent (this, AboutProgram.class); 
     startActivity(intent); 
    } 
} 

我會感謝幫助

+4

請附上錯誤堆棧跟蹤和xml文件 –

+1

也考慮包括'home_layout'的佈局XML文件。 – ishmaelMakitla

回答

0

我認爲,XML文件是正確的,因爲Android的工作室沒有顯示出任何XML錯誤,但我將代碼附加

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



    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="any text" 
     android:textSize="20dp" 
     android:gravity="center" 
     android:layout_marginTop="20dp" 
     android:fontFamily="casual"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="any text" 
     android:textSize="18dp" 
     android:layout_marginTop="5dp" 
     android:gravity="center" 
     android:fontFamily="casual" 
     android:breakStrategy="high_quality" 
     android:shadowColor="#571b1b" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="any text" 
     android:paddingTop="15dp" 
     android:textSize="50dp" 
     android:gravity="center" 
     android:fontFamily="sans-serif-condensed" /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="any text" 
     android:gravity="center_horizontal" 
     android:layout_marginTop="10dp" 
     android:fontFamily="sans-serif-condensed" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="any text" 
     android:layout_marginLeft="110dp" 
     android:onClick="OnNextClick"/> 

     /> 

</LinearLayout> 
+0

首先放置你的'xmlns:android'並且比你的'android:background'',把''''''''附近的''移除''。 – Stanojkovic

0

這是你的XML需要怎麼看起來像:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="#748494" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="any text" 
    android:textSize="20dp" 
    android:gravity="center" 
    android:layout_marginTop="20dp" 
    android:fontFamily="casual"/> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="any text" 
    android:textSize="18dp" 
    android:layout_marginTop="5dp" 
    android:gravity="center" 
    android:fontFamily="casual" 
    android:breakStrategy="high_quality" 
    android:shadowColor="#571b1b" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="any text" 
    android:paddingTop="15dp" 
    android:textSize="50dp" 
    android:gravity="center" 
    android:fontFamily="sans-serif-condensed" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="any text" 
    android:gravity="center_horizontal" 
    android:layout_marginTop="10dp" 
    android:fontFamily="sans-serif-condensed" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="any text" 
    android:layout_marginLeft="110dp" 
    android:onClick="OnNextClick"/> 

</LinearLayout> 
0

感謝大家對xml代碼的幫助。現在的應用程序工作正常,但錯誤的解決方案對我來說很奇怪。在HomeActivity.java中,我刪除了線import android.R;,並且應用程序正在運行。你想告訴我這個解決方案是正確的嗎?