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.
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));
}
});
}
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:
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…