2016-05-02 12 views
0

動作しないコードは、他のレイアウトではうまくいきましたが、他ではありません。..アンドロイドのonClickは

レイアウトXML:

<Button 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="register" 
     android:id="@+id/welcome_register" 
     android:background="@android:color/holo_green_dark" 
     android:textColor="#ffffff" 
     android:textSize="25sp" 
     android:onClick="register_Click" /> 

活動:

package il.co.smartchip.hobby; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 

public class LoginActivity extends AppCompatActivity { 

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

    public void start_login(View view) { 
     //TODO log in 
    } 

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

私はせずにいくつかのことを試してみました成功。それがうまくいかない理由はありますか?

+5

ボタンが正しいレイアウトファイルにありますか? – Prudhvi

+0

意図インテント=新しいインテント(LoginActivity.this、RegisterActivity.class); –

+0

フラグメントが含まれていますか? – njzk2

答えて

0

のようなXMLにonclickのあなたのボタンのInit

<Button 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="register" 
    android:id="@+id/welcome_register" 
    android:background="@android:color/holo_green_dark" 
    android:textColor="#ffffff" 
    android:textSize="25sp" 
    /> 

を削除します。

LoginActivity.thisは、あなたが現在行っているアクティビティのインスタンスを指し、あなたの場合のように動的な内部クラスを扱うときに使用します。

thisは現在のオブジェクト用です。

私はそれほど良い教師ではありません。

0

は、あなたがIntent i = new Intent(LoginActivity.this, RegisterActivity.class);を必要とする、あなたのケースでその

package il.co.smartchip.hobby; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 

public class LoginActivity extends AppCompatActivity { 

private Button myButton; 
private Activity thisActivity=this; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 
    getSupportActionBar().hide(); 
    myButton=(Button)findViewById(R.id.welcome_register); 
    myButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(thisActivity, RegisterActivity.class); 
      startActivity(intent); 
     } 
    }); 
}} 
+4

問題を解決する別の方法を提案することは答えではありません。OPの実装に何が問題なのかを見てください。 – Prudhvi

+1

私は知っているが、私はちょうどこの方法が彼を助けるだろうと思う。申し訳ありません –

+0

と2つのボタンの場合? –

関連する問題