2017-03-04 151 views
1

我創建了一個空白的活動,並有這個主要佈局:activity_main.xml中卡片視圖按鈕適配器

<?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> 

而且我MainActivity.java文件看起來像:

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(); 
    } 
} 

我需要啓動一個新的按下按鈕的活動,此代碼顯示沒有錯誤,但在我的設備上運行它時,它不會啓動LectureActivity。 什麼可能是我的java文件實現onclick可能的變化。 如果這與cardview適配器有什麼關係,那麼我該如何定義適配器類。 我是新來的android編程。感謝任何形式的幫助。謝謝。 enter image description here

回答

0

我找到了我的問題的答案。不知道爲什麼這不起作用,而是,而不是這startActivity(create)我改變它爲MainActivity.this.startActivity(create);它的工作。