MainActivity.java
package cc.starlabs.ifevent_v2;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import cc.starlabs.ifevent_v2.fragment.MainActivityFragment;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate (Bundle savedInstanceState )
{
super.onCreate (savedInstanceState );
setContentView (R.layout.activity_main );
if (savedInstanceState==null )
{
getSupportFragmentManager ().beginTransaction ().add (R.id.fragment,new MainActivityFragment ()).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.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 ();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings )
{
return true;
}
return super.onOptionsItemSelected (item );
}
}
在 Fragment 里使用 MainActivity.this 的时候遇到了 MainActivity is not an enclosing class?
刚开始写 Android...求指教
1
kx5d62Jn1J9MjoXP 2015-08-27 15:02:20 +08:00 1
Java 没学好的结果╮(╯_╰)╭
MainActivity.this 这种引用方式只有在这个类是 MainActivity 的内部类(非 static )的时候才能用 Fragment 里获取 Activity 用 getActivity ()就行了 |
2
qinglangee 2015-08-27 15:12:48 +08:00
@ssynhtn
恩, 现在 0 基础 Android 培训的都只讲一点点 Java 就开始 Android 了 |
4
ruobe 2015-08-27 15:17:33 +08:00
核心竞争力笑尿
|
6
ruobe 2015-08-27 15:24:19 +08:00
|