实际上,“MainActivity.this 不是封闭类”错误是什么?
我在执行以下代码时遇到问题。
实际上,我想使用图像作为指向另一个页面或活动的链接。如何完成
和
“MainActivity.this not in enclosing class”问题到底是什么?这是一个代码
我有2个类,第一个是主要活动
和
第二是主要活动2
两者都在单一主活动.java文件中
主要活动:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.Toast;
import android.content.Intent;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
ImageButton androidImageButton;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
androidImageButton = (ImageButton) findViewById(R.id.imageButton_android);
androidImageButton.setOnClickListener (new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "It works", Toast.LENGTH_LONG).show();
}
});
}
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;
}
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);
}
}
主要活动2:
class MainActivity2 extends MainActivity implements OnClickListener, View.OnClickListener {
ImageButton Btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton Btn = (ImageButton) findViewById(R.id.imageButton_android);
Btn.setOnClickListener(this);
}
@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 void onClick(View v) {
Log.i("clicks","You Clicked B1");
Intent i;
i = new Intent( MainActivity.this, MainActivity2.class); *//The problem is here wih MainActivity.this//
startActivity(i);
}
}
任何人都可以解决错误并帮助我完成代码。