类型MainActivity没有外围实例是制作按钮时,在范围访问 [英] No enclosing instance of the type MainActivity is accessible in scope when making button
问题描述
在努力使Android应用程序,我遇到的错误:类型MainActivity没有外围实例在范围
上线访问 Toast.makeText(MainActivity.this,赃物,Toast.LENGTH_SHORT).show();
这是我的MainActivity.java:
公共类MainActivity扩展ActionBarActivity {
静态按钮BTN; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 如果(savedInstanceState == NULL){
getSupportFragmentManager()调用BeginTransaction()
。新增(R.id.container,新PlaceholderFragment())提交()。
}
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
} / **
*包含一个简单视图中的占位符片段。
* /
公共静态类PlaceholderFragment扩展片段实现View.OnClickListener { 公共PlaceholderFragment(){
} @覆盖
公共无效的onClick(视图v){
Toast.makeText(MainActivity.this斯格,Toast.LENGTH_SHORT).show();
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ 查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);
BTN =(按钮)rootView.findViewById(R.id.test_button);
btn.setOnClickListener(本);
返回rootView;
}
}
}
您必须使用getActivity()的片段中。
要小心,如果你正在做一些后台作业,或当它返回,getActivity任何其他异步任务()可以为空。所以每次你用它检查空。
这是片段的更多信息,你可以使用这个。
When trying to make an android app, I encountered the error:
No enclosing instance of the type MainActivity is accessible in scope
on the line
Toast.makeText(MainActivity.this, "Swag", Toast.LENGTH_SHORT).show();
This is my MainActivity.java:
public class MainActivity extends ActionBarActivity {
static Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements View.OnClickListener {
public PlaceholderFragment() {
}
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Swag", Toast.LENGTH_SHORT).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
btn=(Button) rootView.findViewById(R.id.test_button);
btn.setOnClickListener(this);
return rootView;
}
}
}
You must use getActivity() inside the fragment.
Be careful, if you are doing some background job, or any other async task when it return, getActivity() may be null. So every time you use it check for null.
Further information on fragments, you can use this.
这篇关于类型MainActivity没有外围实例是制作按钮时,在范围访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!