Java 在android studio中无法解析方法';findViewById';

Java 在android studio中无法解析方法';findViewById';,java,android,Java,Android,我通过意图加入两个活动 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.

我通过意图加入两个活动

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;
    }
}

假设您的意思是原始的
findViewById(int)
,而不是您在那里编写的函数:
尝试从活动扩展
这意味着您必须删除未使用的无用功能并导入
android.app.Activity

此外,不要使用
android.support.v7.*
软件包。

从我小小的经验来看,它只会带来麻烦,除非你知道你在用它做什么

假设您指的是原始的
findViewById(int)
,而不是您在那里编写的函数:
尝试从活动扩展
这意味着您必须删除未使用的无用功能并导入
android.app.Activity

此外,不要使用
android.support.v7.*
软件包。

从我小小的经验来看,它只会带来麻烦,除非你知道你在用它做什么

这可能与Android Studio有关。尝试清理项目,然后重建。如果不起作用,请转到文件->使缓存无效/重新启动


我有时也会遇到这些问题。让我知道这是否有效。我试过你的代码,效果很好

这可能与Android Studio有关。尝试清理项目,然后重建。如果不起作用,请转到文件->使缓存无效/重新启动


我有时也会遇到这些问题。让我知道这是否有效。我试过你的代码,效果很好

问题似乎是您定义了一个名为:
findviewbyd(int按钮)
的方法,该方法始终返回0

使用“活动”方法而不是您自己的方法:

this.findViewById(int-resourceId)


祝你好运

问题似乎是您定义了一个名为:
findviewbyd(int按钮)
的方法,该方法始终返回0

使用“活动”方法而不是您自己的方法:

this.findViewById(int-resourceId)


祝你好运

您似乎正在实现自己的findviewbyid()。我不知道你是否打算这样做

尝试删除

private int findviewbyid(int btnMsg) {
}

ActionBarActivity的findviewbyid应该从布局文件中解析您的按钮。

看起来您正在实现自己的findviewbyid()。我不知道你是否打算这样做

尝试删除

private int findviewbyid(int btnMsg) {
}

ActionBarActivity的findviewbyid应该可以从布局文件中解决您的按钮问题。

请更全面地描述问题,并且只发布代码的相关部分。您是在尝试使用自己的
findviewbyid
还是只是一次尝试?请更全面地描述问题,并且只发布代码的相关部分。您是在尝试使用自己的
findViewById
还是只是一次尝试?我不知道您的建议如何解决问题。ActionBarActivity也有findViewById()。我不知道这些建议如何解决这个问题。此外,v7版本所能提供的支持在Android版本中更为一致。我不认为你的建议能解决这个问题。ActionBarActivity也有findViewById()。我不知道这些建议如何解决这个问题。此外,v7版本所能提供的支持在Android版本中更为一致。