java 导入 android.content.Context 后无法解析上下文;还
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29338633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot resolve context after importing android.content.Context; also
提问by richapathak
This is my mainactivity.java
class
这是我的mainactivity.java
课
package com.example.apurva.therisingsatyam;
import android.os.Bundle;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(context, LogIn.class);
startActivity(intent);
break;
case R.id.button2:
Intent intent1 = new Intent(context, SignUp.class);
startActivity(intent1);
break;
}
}
}
I am trying to switch activity when the button is clicked.But as can be seen in above code there is a red line under context word at both the places in onClick method, although I have imported android.content.context . Please someone help me out to solve this issue.
我试图在单击按钮时切换活动。但正如在上面的代码中所见,在 onClick 方法的两个位置的上下文词下都有一条红线,尽管我已经导入了 android.content.context 。请有人帮我解决这个问题。
回答by Raghunandan
context
is not declared and initialized
context
未声明和初始化
Change this
改变这个
Intent intent = new Intent(context, LogIn.class);
to
到
Intent intent = new Intent(MainActivity.this, LogIn.class);
Similarly for the other intent
同样对于另一个意图
Your MainActivity extends Activity
您的 MainActivity 扩展了 Activity
java.lang.Object
? android.content.Context
? android.content.ContextWrapper
? android.view.ContextThemeWrapper
? android.app.Activity
So to get context you can have MainActivity.this
.
因此,要获得上下文,您可以拥有MainActivity.this
.
Also i don't see where you initiailze your views and your Activity
does not implement OnClickListener
interface
我也没有看到你在哪里初始化你的视图并且你Activity
没有实现OnClickListener
接口
回答by Pratik Dasa
context is not declared by you, so I suggest you to use following lines of code:
上下文不是由您声明的,因此我建议您使用以下代码行:
Intent intent = new Intent(MainActivity.this, LogIn.class);//MainActivity.this contains your current context
startActivity(intent);
Still any doubt then feel free to ask.
仍然有任何疑问,然后随时问。