![](/img/trans.png)
[英]Android: cannot resolve method 'findViewById(int)' in AsyncTask
[英]Cannot resolve method 'findViewByID(int)' in Android Studio
我才剛剛開始為 androud 編寫代碼,但我一直在 android studio 中收到錯誤:“無法解析方法‘findViewByID(int)’”
我似乎無法找出問題所在,嘗試設置 contentView,實現 OnClickListenener,但這些都沒有解決任何問題。
下面的完整代碼,MainActivity 方法是一切發生的地方:
package aprivate.contract.jdeko.dww_registration;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends AppCompatActivity{
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clickButton = (Button) findViewByID(R.id.Btn);
clickButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
//todo
}
});
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://aprivate.contract.jdeko.dww_registration/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://aprivate.contract.jdeko.dww_registration/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
這只是您的錯字。 您應該使用“ Id”代替“ ID”。
所以你
Button clickButton = (Button) findViewByID(R.id.Btn);
成為
Button clickButton = (Button) findViewById(R.id.Btn);
使用findViewById()
代替findViewByID()。 幾乎沒有拼寫錯誤
這就是問題:
Button clickButton = (Button) findViewByID(R.id.Btn);
更改為此:
Button clickButton = (Button) findViewById(R.id.Btn);
它是 findViewById
“Id” D 應該是小寫字母,但在你的情況下它是大寫的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.