我刚刚开始为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();
}
}
答案 0 :(得分:2)
这只是你犯过的错字。你应该使用" Id"而不是" ID"。
所以你的
Button clickButton = (Button) findViewByID(R.id.Btn);
变为
Button clickButton = (Button) findViewById(R.id.Btn);
答案 1 :(得分:0)
使用findViewById()
代替findViewByID()。没有拼写错误
答案 2 :(得分:0)
这是问题所在:
Button clickButton = (Button) findViewByID(R.id.Btn);
将其更改为:
Button clickButton = (Button) findViewById(R.id.Btn);