得到错误"无法解决findViewById(int)的' [英] Getting error "cannot resolve findViewById(int)'
问题描述
我有一个主要活动,当我preSS一个按钮,我浏览到刷卡选项卡视图。现在,我想在这些选项卡和present一个可点击按钮敬酒时,按钮pssed $ P $。这里是code我使用:
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;公共类洗衣扩展片段{
按钮btn_service;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根
返回inflater.inflate(R.layout.activity_laundry,集装箱,FALSE);
btn_service =(按钮)findViewById(R.id.btn_service);
btn_service.setOnClickListener(本);
}
但findViewById不是一个符号知道这个活动。有没有纳入片段活动点击按钮类似上面的另一种方式?
然的项目,我得到这个错误。 (我不能知道这个建议的修改工作,我的应用程序崩溃,下面的错误),这是一个片段活动,我想一个按钮被点击时生成敬酒。
公共类in_room_dining扩展片段
{
按钮btn_order;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState)
{
// TODO自动生成方法存根 btn_order.setOnClickListener(新OnClickListener()
{
公共无效的onClick(视图v)
{
Toast.makeText(getActivity(),文字,Toast.LENGTH_SHORT).show();
}
});
返回inflater.inflate(R.layout.activity_in_room_dining,集装箱,FALSE);
}
}
我得到的错误是:
10月11日至29日:57:42.820 938-938 / com.appt.shreyabisht.staymax E / AndroidRuntime:致命异常:主要
工艺:com.appt.shreyabisht.staymax,PID:938
显示java.lang.NullPointerException
在com.appt.shreyabisht.staymax.in_room_dining.onCreateView(in_room_dining.java:23)
在android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
(in_room_dining.java:23)是btn_order.setOnClickListener(新OnClickListener()
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;公共类洗衣扩展片段{
按钮btn_service;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根
最后查看rootView = inflater.inflate(R.layout.activity_laundry,集装箱,
假); btn_service =(按钮)rootView.findViewById(R.id.btn_service);
btn_service.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){ //你的code放这里
}
});
返回rootView;
}
I have a main activity and when I press a button, i am navigated to a swipe Tab view. Now I want to have a Clickable Button in these tabs and present a toast when the button is pressed. Here is the code I use:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class laundry extends Fragment {
Button btn_service;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.activity_laundry, container,false);
btn_service = (Button) findViewById(R.id.btn_service);
btn_service.setOnClickListener(this);
}
But the findViewById is not a know symbol to this activity. Is there an alternative way to incorporate clickable buttons in fragment activity like above?
Ran the project and I get this error. ( I cant know if the suggested changes worked, my app crashed with the following error) This is a fragment activity and I am trying to generate a toast when a button is clicked.
public class in_room_dining extends Fragment
{
Button btn_order;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// TODO Auto-generated method stub
btn_order.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getActivity(), "text", Toast.LENGTH_SHORT).show();
}
});
return inflater.inflate(R.layout.activity_in_room_dining, container, false);
}
}
The error I got is :
11-29 10:57:42.820 938-938/com.appt.shreyabisht.staymax E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.appt.shreyabisht.staymax, PID: 938
java.lang.NullPointerException
at com.appt.shreyabisht.staymax.in_room_dining.onCreateView(in_room_dining.java:23)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
(in_room_dining.java:23) is btn_order.setOnClickListener(new OnClickListener()
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class laundry extends Fragment {
Button btn_service;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
final View rootView = inflater.inflate(R.layout.activity_laundry, container,
false);
btn_service = (Button) rootView.findViewById(R.id.btn_service);
btn_service.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//your Code Goes Here
}
});
return rootView;
}
这篇关于得到错误"无法解决findViewById(int)的'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!