简体   繁体   English

开始一个新的活动Android OnClickListener

[英]Start a new activity OnClickListener Android

I am having trouble with using OnClickListener. 我在使用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. 我的应用程序在运行时就崩溃了。

In my LaunchActivity.java: 在我的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));
        }
    });
}

In my activity_launch.xml: 在我的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. 谢谢您的帮助,这是我的第一个应用程序。

try this 尝试这个

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

instead 代替

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

hope it help 希望对你有帮助

请在此处分享您的logcat错误。并尝试将对象从Button更改为com.getbase.floatingactionbutton.FloatingActionButton。

Declare your activity in manifest file as follows 在清单文件中声明您的活动,如下所示

<activity
        android:name=".CreateNote"
        android:label="@string/app_name">
    </activity>

Ok, you got it working, but for your information, the reason why you were facing this problem was, the FAB is NOT one of those simple buttons that you create. 好吧,你懂了工作,但对您的信息,为什么你面临这样的问题是原因,FAB是不是你创建这些简单的按钮中的一个。 You have to create an object of floatingActionButton instead of creating the object of button (which you did in your case). 你必须创建一个对象floatingActionButton而不是创建的对象的button (你在你的情况一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM