开始一个新的活动OnClickListener的Andr​​oid [英] Start a new activity OnClickListener Android

查看:202
本文介绍了开始一个新的活动OnClickListener的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用OnClickListener麻烦。我想,当用户presses的一个按钮在我的浮动操作按钮,启动一个新的活动。当我运行我的应用程序崩溃只是右。

I am having trouble with using OnClickListener. I am trying to start a new activity when the user presses one of the buttons in my floating action button. My app just crashes right when I run it.

在我LaunchActivity.java:

In my LaunchActivity.java:

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

    final Button buttonNote = (Button) findViewById(R.id.note);

    buttonNote.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            startActivity(new Intent(LaunchActivity.this, CreateNote.class));
        }
    });
}

在我activity_launch.xml:

In my activity_launch.xml:

<com.getbase.floatingactionbutton.FloatingActionsMenu
    android:id="@+id/create"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    fab:fab_addButtonColorNormal="@color/accent"
    fab:fab_addButtonColorPressed="@color/accent_dark"
    fab:fab_addButtonPlusIconColor="@color/window_background"
    fab:fab_labelStyle="@style/menu_labels_style"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp">
    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/note"
        android:src="@drawable/ic_note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingBottom="16dp"
        fab:fab_title="@string/note"
        fab:fab_colorNormal="@color/accent"
        fab:fab_colorPressed="@color/accent_dark"/>
</com.getbase.floatingactionbutton.FloatingActionsMenu>

CreateNote.java:

CreateNote.java:

public class CreateNote extends Activity {

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


@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_create_note, 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);
}
}

感谢您的帮助,这是我的第一个应用程序。

Thank you for any help, this is my first app.

推荐答案

试试这个

findViewById(R.id.note).setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    startActivity(new Intent(LaunchActivity.this, CreateNote.class));
  }
});

而不是

final Button buttonNote = (Button) findViewById(R.id.note);

buttonNote.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        startActivity(new Intent(LaunchActivity.this, CreateNote.class));
    }
});

希望它能帮助

这篇关于开始一个新的活动OnClickListener的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆