Java 按钮Id未解析
我的java组件声明存在未知类,但我在xml文件中清楚地声明了新id content\u user\u response.xmlJava 按钮Id未解析,java,android,button,Java,Android,Button,我的java组件声明存在未知类,但我在xml文件中清楚地声明了新id content\u user\u response.xml <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" android:id="@+id/gotom
<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" />
我需要帮助。提前感谢您的代码应该在onCreate
中,因为调用设置内容视图
时视图会膨胀:
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.gotomoodanalysis);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_response);
gotomoodanalysis = (Button)findViewById(R.id.gotomoodanalysis);
}
作废setContentView(视图视图)
将活动内容设置为显式视图。此视图已放置
直接进入活动的视图层次结构。它本身可以是一个
复杂视图层次结构。调用此方法时,布局
将忽略指定视图的参数
Androidvoid onCreate(Bundle savedInstanceState)在活动启动时调用
void onCreate(Bundle savedInstanceState)
是大多数初始化应该去的地方
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_response);
gotomoodanalysis = (Button)findViewById(R.id.gotomoodanalysis); // You should initialize in here
}
你们应该阅读官方指南
提供错误日志。activity\u user\u response.xml
是什么样子的?@theblindprophet您真的需要日志猫来查找此代码段中的错误吗??:oA问题需要提供所有补充信息。描述错误可能会导致与实际提供错误本身截然不同的解释。你的玩笑毫无意义。因此,所有用户都有机会回答问题,而不仅仅是那些有标记语言经验的用户。这个问题对一些人来说可能是“显而易见的”,但对其他想回答这个问题的人来说则不然。回答得好,一定是我的+1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_response);
gotomoodanalysis = (Button)findViewById(R.id.gotomoodanalysis); // You should initialize in here
}