2015-12-23 6 views
9

Ich lerne Android mit http://developer.android.com/index.html, aber wenn ich http://developer.android.com/training/basics/firstapp/starting-activity.html Display die Meldung Abschnitt erreichen, Punkt 6 Ich habe einen Fehlerandroid.support.v7.widget.AppCompatTextView kann nicht auf android.widget.RelativeLayout gegossen werden

android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.RelativeLayout 

Ich habe alles Schritt für Schritt gemacht wie im Tutorial. Warum funktioniert es nicht? Sie definieren RelativeLayout mit der ID von Textview

DisplayMessageActivity.java 

    package com.example.flover.hellloworld; 

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

     RelativeLayout layout = (RelativeLayout) findViewById(R.id.content); 
     layout.addView(textView); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_display_message, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      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" 
    tools:context="com.example.flover.hellloworld.DisplayMessageActivity"> 

    <TextView 
     android:id="@+id/content" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ></TextView> 

</RelativeLayout> 

Antwort

11

Alles, was Sie tun müssen, ist Assign Id Relative-Layout sollte es so sein

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rl_Container" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"></TextView> 

</RelativeLayout> 

dann nur eine geringfügige Änderung Ihrer Tätigkeit machen

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

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl_Container); 
    layout.addView(textView); 
} 


} 
+0

Für mich das android gerade hinzufügen : ID zum root Layout Container hat mein Problem behoben. –

3
RelativeLayout layout = (RelativeLayout) findViewById(R.id.content); 

die problem.because verursacht.

Assign

android:id="@+id/content" 

zu RelativeLayout. Nicht zu TextView in XML

3

Ändern Sie Ihr Layout zu diesem, sollte die ID-Inhalt auf die RelativeLayout und nicht die TextView gehen.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/content" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.flover.hellloworld.DisplayMessageActivity"> 

    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

</RelativeLayout> 
Verwandte Themen