2017-04-27 65 views
-2

我在Android Studio中是新的,我想按鈕鏈接到其他網頁, 但我唯一的問題是OnClickListener,請大家幫忙;(OnClickListener,着其鏈接到另一個XML頁面佈局

標題的.java(主頁,page_title.xml)

package com.afinal.xxx.learnk; 

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

import static com.afinal.xxx.learnk.R.styleable.View; 

public class Title extends AppCompatActivity { 

static { 
    System.loadLibrary("native-lib"); 
} 

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

    Button button = (Button)findViewById(R.id.title_learn); 
    button.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(this, Learn.class); 
      startActivity(i); 
     } 
    }); 
}; 

public native String stringFromJNI(); 
} 

^^標題XML的按鈕(RelativeLayout的)

<Button 
    android:id="@+id/title_learn" 
    android:layout_width="200dp" 
    android:layout_height="100dp" 
    android:layout_alignStart="@+id/title_logo" 
    android:layout_below="@+id/title_logo" 
    android:layout_column="0" 
    android:layout_marginTop="41dp" 
    android:layout_row="2" 
    android:onClick="start" 
    android:text="LEARN" 
    android:textSize="36sp" 
    android:textStyle="bold" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintHorizontal_bias="0.496" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    tools:layout_constraintBottom_creator="1" 
    tools:layout_constraintLeft_creator="1" 
    tools:layout_constraintRight_creator="1" /> 

Learn.java(下一頁,page_learn.xml)

package com.afinal.xxx.learnk; 

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

public class Learn extends AppCompatActivity { 

static { 
    System.loadLibrary("native-lib"); 
} 

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

} 

我使用的Android API 19更好的兼容性,是有這個問題沒有其他解決辦法?非常感謝:D

+1

是什麼問題? –

+0

您是否聲明瞭在Manifest文件中學習活動? – Nawaf

回答

0

邏輯正確,但代碼錯誤。

  1. 檢查你的代碼onclickListener

    Intent i = new Intent(this, Learn.class); 
    

這是不對的,使用Title.this而不是這個。

2.檢查您的Manifest.xml 在Manifest.xml中註冊Learn.class。

0

您必須將「this」改爲Title.this爲 ,並且在創建函數的花括號後面有一個額外的分號。

只要複製粘貼此並親自嘗試

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

Button button = (Button)findViewById(R.id.title_learn); 
button.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     Intent i = new Intent(this, Learn.class); 
     startActivity(i); 
    } 
}); 
} 
相關問題