我已经在这些错误中苦苦挣扎了好几天,我试着用谷歌搜索它,但遗憾的是我找不到任何修复方法。 我还是Java for Java的初学者。
所以我得到了一个带有片段的NavigationDrawer,在那个片段中我想放一个可点击的按钮,但不知何故它只是没有识别按钮和一些东西而我已经在我的布局。
这是我的代码:
package nl.c99.c99nlapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Souf on 21-9-2015.
*/
public class First_Fragment extends Fragment {
View MyView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyView = inflater.inflate(R.layout.first_layout, container, false);
return MyView;
View rootView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) rootView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onClick(v);//THIS IS THE METHOD YOU WROTE ON THE ATTACHED CODE!!
}
});
}
}

我的项目截图:
First_Fragment Class http://i.stack.imgur.com/5W2uG.png
first_fragment的布局: http://i.imgur.com/ndVmcX2.png
我收到以下错误:
答案 0 :(得分:1)
无法解析符号'OnClickListener'(新的OnClickListener)
在导入中添加此内容:import android.view.View.OnClickListener;
您只需选择一个观点,因为您有2个我不知道为什么rootView
和MyView
然后您的onClickListener()
应该是:
Button c = (Button) MyView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//your stuff here
}
});
从不使用参数'v'。 (查看v)
只需删除此onClick(v)
如果你这样做,你的coude应该有效:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) MyView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Stuff there
}
});
return MyView;
}
package nl.c99.c99nlapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
/**
* Created by Souf on 21-9-2015.
*/
public class First_Fragment extends Fragment {
View MyView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) MyView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Stuff there
}
});
return MyView;
}
}
答案 1 :(得分:0)
您必须在代码末尾返回rootView,因为返回意味着"结束该方法" ,因此它不会继续下面的代码同样的方法。
用以下代码替换您的代码:
package nl.c99.c99nlapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class First_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) rootView.findViewById(R.id.button2);
c.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Write your code here.
}
});
return rootView;
}
}