Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

java - Start a new activity OnClickListener Android

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...