gpt4 book ai didi

java - findViewById 在 Fragment 中找不到 ImageButton

转载 作者:行者123 更新时间:2023-12-02 10:54:25 24 4
gpt4 key购买 nike

我正在尝试在我正在创建的信息应用程序的一个 fragment 中流式传输视频。当我创建图像按钮来控制视频时,findViewByIdbtnPlayPause = (ImageButton)findViewById(R.id.play_pause_button);< 行上给出了“无法解析符号”错误,但是它可以在同一 fragment 中找到视频 View 。这是我的 MainActivity.java 中的 onCreate 方法:

public class MainActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout drawer;
ProgressDialog mDialog;
VideoView videoView;
ImageButton btnPlayPause;

String videoURL = "https://vimeo.com/284762179";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer,
toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

if (savedInstanceState == null { getSupportFragmentManager()
.beginTransaction().replace(R.id.fragment_containter,
new InformationFragment()).commit();
navigationView.setCheckedItem(R.id.nav_information);
}

videoView = (VideoView)findViewById(R.id.hypori_video);
btnPlayPause = (ImageButton)findViewById(R.id.play_pause_button);
btnPlayPause.setOnClickListener(this);
}

@Override
public void onClick(View v){
mDialog = new ProgressDialog(MainActivity.this);
mDialog.setMessage("Please wait...");
mDialog.setCanceledOnTouchOutside(false);
mDialog.show();

try {
if (!videoView.isPlaying()) {
Uri uri = Uri.parse(videoURL);
videoView.setVideoURI(uri);
videoView.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
btnPlayPause.setImageResource(R.drawable.ic_play_arrow);
}
});
}
else
{
videoView.pause();
btnPlayPause.setImageResource(R.drawable.ic_play_arrow);
}
}
catch (Exception e)
{

}
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mDialog.dismiss();
mp.setLooping(false);
videoView.start();
btnPlayPause.setImageResource(R.drawable.ic_pause);
}
});
}

这是我的包含 VideoView 和 ImageButton 的 fragment 的 XML:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light">

<TextView
android:id="@+id/the_hypori_solution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#b41518"
android:padding="10dp"
android:text="@string/the_hypori_solution"
android:textAlignment="center"
android:textColor="@android:color/background_light"
android:textSize="30sp"
android:textStyle="bold" />

<ScrollView
android:id="@+id/solution_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/the_hypori_solution">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">

<TextView
android:id="@+id/virtualMobileInfrastructure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/virtual_mobile_infrastructure"
android:textColor="@android:color/black"
android:textSize="30sp"
android:textStyle="italic" />

<TextView
android:id="@+id/vmi_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/virtualMobileInfrastructure"
android:layout_marginTop="10dp"
android:text="@string/vmi_info"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="20sp" />

<VideoView
android:id="@+id/hypori_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/vmi_info"
android:layout_centerHorizontal="true"/>

<ImageButton
android:id="@+id/play_pause_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hypori_video"
android:layout_centerHorizontal="true"
app:srcCompat="@drawable/ic_play_arrow"/>

<ImageView
android:id="@+id/hypori_solution_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/play_pause_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="10dp"
android:contentDescription="@string/hypori_solution_image"
app:srcCompat="@drawable/hypori_solution__vertical_edited"/>

<TextView
android:id="@+id/solution_tv_vmi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hypori_solution_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="@string/components_of_the_vmi_platform"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="30sp" />

<ImageView
android:id="@+id/hypori_client_vmi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/solution_tv_vmi"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:contentDescription="@string/hypori_client"
app:srcCompat="@drawable/hypori_client" />

<TextView
android:id="@+id/hypori_client_vmi_tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/hypori_client_vmi"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/hypori_client_vmi"
android:layout_toRightOf="@+id/hypori_client_vmi"
android:text="@string/hypori_client"
android:textColor="@android:color/black"
android:textSize="21sp"
android:textStyle="bold" />

<TextView
android:id="@+id/hypori_client_vmi_tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/hypori_client_vmi_tv1"
android:layout_alignStart="@+id/hypori_client_vmi_tv1"
android:layout_below="@+id/hypori_client_vmi_tv1"
android:layout_marginTop="5dp"
android:text="@string/"
android:textColor="@android:color/black"
android:textSize="17sp" />

<ImageView
android:id="@+id/hypori_vmd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/hypori_client_vmi_tv2"
android:layout_marginTop="30dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:contentDescription="@string/"
app:srcCompat="@drawable/" />

<TextView
android:id="@+id/hypori_vmd_tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/hypori_client_vmi_tv1"
android:layout_alignStart="@+id/hypori_client_vmi_tv1"
android:layout_alignTop="@+id/hypori_vmd"
android:text="@string/hypori_virtual_mobile_device"
android:textColor="@android:color/black"
android:textSize="21sp"
android:textStyle="bold" />

<TextView
android:id="@+id/hypori_vmd_tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/hypori_client_vmi_tv1"
android:layout_alignStart="@+id/hypori_client_vmi_tv1"
android:layout_below="@+id/hypori_vmd_tv1"
android:layout_marginTop="5dp"
android:text="@string/
android:textColor="@android:color/black"
android:textSize="17sp" />

<ImageView
android:id="@+id/hypori_admin_console_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/hypori_vmd_tv2"
android:layout_marginTop="30dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:contentDescription="@string/hypori_admin_console"
app:srcCompat="@drawable/hypori_admin_console" />

<TextView
android:id="@+id/hypori_admin_console_tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/hypori_client_vmi_tv1"
android:layout_alignStart="@+id/hypori_client_vmi_tv1"
android:layout_alignTop="@+id/hypori_admin_console_iv"
android:text="@string/hypori_admin_console"
android:textColor="@android:color/black"
android:textSize="21sp"
android:textStyle="bold" />

<TextView
android:id="@+id/hypori_admin_console_tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/hypori_client_vmi_tv1"
android:layout_alignStart="@+id/hypori_client_vmi_tv1"
android:layout_below="@+id/hypori_admin_console_tv1"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="@string/
android:textColor="@android:color/black"
android:textSize="17sp" />

</RelativeLayout>

</LinearLayout>

</ScrollView>

最佳答案

将 Activity 类的 header 更改为:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener

您忘记实现View.OnClickListener,因此onClick()方法不会覆盖任何内容

关于java - findViewById 在 Fragment 中找不到 ImageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51883343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com