按钮ID未解决

pin*_*ffy 0 java android button

我的java组件声明有一个未知的类,但我清楚地在我的xml文件中声明了新的id

content_user_response.xml

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/gotomoodanalysis"
    android:layout_below="@+id/frame4"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
Run Code Online (Sandbox Code Playgroud)

UserResponse.java

package com.example.enxin.emotiontracker;

import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;


public class UserResponse extends AppCompatActivity {
    Button gotomoodanalysis;

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

    gotomoodanalysis = (Button)findViewById(R.id.next1);

}
Run Code Online (Sandbox Code Playgroud)

我需要帮助.提前致谢

All*_*ira 5

你的代码应该在里面 onCreate,因为在setContentView调用时视图会膨胀:

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

    gotomoodanalysis = (Button)findViewById(R.id.gotomoodanalysis);
}
Run Code Online (Sandbox Code Playgroud)