View Binding无法解析textview_first ID的问题求助
View Binding无法解析textview_first ID的问题求助
我是个编程新手,现在碰到一个头疼的问题:下面这行代码还没等我运行程序,IDE就直接报“cannot find symbol”错误,说textviewFirst是未知的:
TextView textView = binding.textviewFirst;
以下是我的MainActivity.java完整代码:
package com.example.rp; import android.os.Bundle; import com.google.android.material.snackbar.Snackbar; import androidx.appcompat.app.AppCompatActivity; import android.view.View; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import com.example.rp.databinding.ActivityMainBinding; import android.view.Menu; import android.view.MenuItem; import com.example.rp.ApiService; import com.example.rp.Post; import java.util.List; import retrofit2.Retrofit; // Import Retrofit import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.Call; // Import Call import retrofit2.Callback; // Import Callback import retrofit2.Response; // Import Response import android.widget.TextView; public class MainActivity extends AppCompatActivity { private AppBarConfiguration appBarConfiguration; private ActivityMainBinding binding; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); //TextView textView = findViewById(R.id.textview_first); TextView textView = binding.textviewFirst; setSupportActionBar(binding.toolbar); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main); appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build(); NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); binding.fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAnchorView(R.id.fab) .setAction("Action", null).show(); } }); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://jsonplaceholder.typicode.com/") // Base URL .addConverterFactory(GsonConverterFactory.create()) // Gson converter for JSON parsing .build(); // Create the API service instance ApiService apiService = retrofit.create(ApiService.class); // Make the network request Call<List<Post>> call = apiService.getPosts(); // Execute the call (synchronously for simplicity here) call.enqueue(new Callback<List<Post>>() { @Override public void onResponse(Call<List<Post>> call, Response<List<Post>> response) { if (response.isSuccessful()) { List<Post> posts = response.body(); // Get the list of users try { textView.setText(posts.toString()); } catch (Exception e) { e.printStackTrace(); } // // Handle the response here } } @Override public void onFailure(Call<List<Post>> call, Throwable t) { // Handle failure } }); } @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 boolean onSupportNavigateUp() { NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main); return NavigationUI.navigateUp(navController, appBarConfiguration) || super.onSupportNavigateUp(); } }
这个出问题的TextView是定义在fragment_first.xml布局文件里的,代码如下:
<?xml version="1.0" encoding="utf-8"?> <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".FirstFragment"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <Button android:id="@+id/button_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" app:layout_constraintBottom_toTopOf="@id/textview_first" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textview_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="@string/lorem_ipsum" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/button_first" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.core.widget.NestedScrollView>
特别奇怪的是,如果我换成findViewById的方式来获取这个TextView,IDE就能正常识别这个ID,代码完全没问题:
//TextView textView = findViewById(R.id.textview_first);
备注:内容来源于stack exchange,提问作者Anunnaki Priest
相关产品推荐
相关产品推荐

