[英]In android studio cannot resolve method 'findViewById'
I am joining two activity through intent 我通过意图参加了两项活动
package com.smartcodeone.newapp1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
public static final String STRING_VAR = "com.smartcodeone.newapp1.HELLO_WORLD";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnMsg = (Button) findViewById(R.id.btnMsg);
btnMsg.setOnClickListener(new View.OnClickListener(){
//when user click's this function will be called
public void onClick(View v){
Intent intentvar = new Intent(getApplicationContext(),Main2Activity.class);
intentvar.putExtra(STRING_VAR,"Hello World"); //this is used to pass data to next intent
startActivity(intentvar);
}
});
}
private int findViewId(int btnMsg) {
return 0;
}
@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);
}
@Override
public android.support.v4.app.FragmentManager getSupportFragmentManager() {
return null;
}
}
It might have to do with Android Studio. 这可能与Android Studio有关。 Try cleaning your project and then rebuilding.
尝试清理您的项目,然后重建。 If that doesn't work go to File -> Invalidate Caches/Restart ...
如果这不起作用,请转到“文件”->“使缓存无效/重新启动...”
I get those issues sometimes as well. 有时我也会遇到这些问题。 Let me know if that works.
让我知道是否可行。 I tried your code and it works fine.
我尝试了您的代码,它工作正常。
The issue seems to be that you defined a method named: findViewById(int button)
that always return 0. 问题似乎是您定义了一个名为:
findViewById(int button)
的方法,该方法始终返回0。
Use the Activity method instead of your own: 使用Activity方法而不是您自己的方法:
this.findViewById(int resourceId)
Good luck! 祝好运!
It seems you are implementing your own findviewbyid(). 看来您正在实现自己的findviewbyid()。 I don't know if you intend to do so.
我不知道你是否打算这样做。
Try removing 尝试移除
private int findviewbyid(int btnMsg) {
}
The ActionBarActivity's findviewbyid should resolve your button from your layout file. ActionBarActivity的findviewbyid应该可以从布局文件中解析您的按钮。
Assuming you meant to the original findViewById(int)
and not the function you wrote there: 假设您要使用原始的
findViewById(int)
而不是您在此处编写的函数:
Try extending from Activity instead. 尝试从Activity扩展。
That means that you will have to delete the useless function you didn't use and import android.app.Activity
. 这意味着您将必须删除未使用的无用功能,并导入
android.app.Activity
。
In Addition, don't use the android.support.v7.*
package. 此外,请勿使用
android.support.v7.*
包。
From my little experience it only causes troubles, unless you know what are you doing with it. 根据我的经验,除非您不知道自己在做什么,否则它只会带来麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.