2016-05-25 64 views
0

我通过意向Android Studio中无法解析法“findViewById”

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

请更全面地描述问题,只发布代码的相关部分。 – sschale

+0

你想使用你自己的'findViewById'吗?或者它只是你做的一个尝试? – Amirag

回答

-1

连接两个活动。假设你的意思是原来的findViewById(int),而不是你写那里的功能:
尝试,而不是从活动延伸。
这意味着您将不得不删除您没有使用的无用功能并导入android.app.Activity

另外,不要使用android.support.v7.*包。
从我的小经验来看,它只会导致麻烦,除非你知道你在做什么。

+1

我不明白你的建议如何解决问题。 ActionBarActivity也有findViewById()。 – Egor

+0

我不明白这些建议如何解决问题。此外,支持v7版本可以创建更安全的Android版本体验。 – cyroxis

2

它可能与Android Studio有关。尝试清理你的项目,然后重建。如果这不起作用去文件 - >无效缓存/重启...

我有时也得到这些问题。让我知道这是否有效。我试过你的代码,它工作正常。

0

这个问题似乎是你定义了一个名为方法:findViewById(int button)总是返回0。

使用活动方法,而不是你自己的:

this.findViewById(int resourceId)

祝你好运!

0

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

尝试删除

private int findviewbyid(int btnMsg) { 
} 

的ActionBarActivity的findviewbyid应该从你的布局文件解决您的按钮。

相关问题