2017-03-04 3 views
1

Ich habe eine leere Aktivität und mit diesem Haupt Layout: activity_main.xmlKarte Anzeigen Button Adapter

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    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.solution.engineering.lecture_time.MainActivity"> 
    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="4dp" 
     android:id="@+id/note"> 


     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="@dimen/activity_horizontal_margin"> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/title" 
       android:text="@string/welcome" 
       android:textAppearance="?android:textAppearanceLarge"/> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/description" 
       android:text="@string/description" 
       android:textAppearance="?android:textAppearanceMedium" 
       android:layout_below="@+id/title"/> 

     </RelativeLayout> 
    </android.support.v7.widget.CardView> 
    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="4dp" 
     android:layout_below="@id/note"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="@dimen/activity_horizontal_margin"> 
      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:text="@string/button_text" 
       android:id="@+id/button_lecture"> 


      </Button> 

     </RelativeLayout> 


    </android.support.v7.widget.CardView> 


</RelativeLayout> 

Und meine MainActivity.java Datei wie folgt aussieht:

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 

    public Button lecture; 
    public void setnew(){ 
     lecture=(Button)findViewById(R.id.lecture_button); 
     lecture.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent create = new Intent(MainActivity.this,LectureActivity.class); 
       startActivity(create); 

      } 
     }); 

    } 


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

ich eine neue starten müssen Aktivität vom Button-Klick, dieser Code zeigt keinen Fehler an, aber während er auf meinem Gerät ausgeführt wird, wird die LectureActivity nicht gestartet. Was könnten die möglichen Änderungen in meiner Java-Datei sein, um den Onclick zu implementieren? Wenn das etwas mit Adapter für cardview zu tun hat, wie definiere ich dann die Adapterklasse. Ich bin neu in Android-Programmierung. Schätze jede Art von Hilfe. Danke. enter image description here

Antwort

0

Ich fand die Antwort auf meine Frage. Ich weiß nicht, warum das nicht funktioniert, aber statt startActivity(create) habe ich es in MainActivity.this.startActivity(create); geändert und es hat funktioniert.

Verwandte Themen