MainActivity必须实现OnFragmentInteractionListener(MainActivity must implement OnFragmentInteractionListener)
我已经添加了片段到android avigation抽屉活动并得到了这个错误,我正在按照教程和完全相同的东西在教程中工作,我错过了什么?
java.lang.RuntimeException: app.com.navigatemenu.MainActivity@56e7d2c must implement OnFragmentInteractionListener at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method)
主要活动
package app.com.navigatemenu; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.view.View; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { NavigationView navigationView = null; Toolbar toolbar = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MainFragment fragment = new MainFragment(); android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.fragment_container, fragment); fragmentTransaction.commit(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.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); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camera) { MainFragment fragment = new MainFragment(); android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.fragment_container, fragment); fragmentTransaction.commit(); // Handle the camera action } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } }
不能弄清楚什么是错的。 有人可以帮忙,感激你的时间。
I have added fragment to android avigation drawer activity and got this error , I was following a tutorial and exact same thing worked in tutorial, am i missing something?
java.lang.RuntimeException: app.com.navigatemenu.MainActivity@56e7d2c must implement OnFragmentInteractionListener at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method)
Main Activity
package app.com.navigatemenu; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.view.View; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { NavigationView navigationView = null; Toolbar toolbar = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MainFragment fragment = new MainFragment(); android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.fragment_container, fragment); fragmentTransaction.commit(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.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); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camera) { MainFragment fragment = new MainFragment(); android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.fragment_container, fragment); fragmentTransaction.commit(); // Handle the camera action } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } }
Don't able to figure out what wrong. can someone please help, appreciate your time.
原文:https://stackoverflow.com/questions/41317332
最满意答案
在这5秒睡眠期间,您将无法取消任务。 取消只会在
ThrowIfCancellationRequested()
调用时发生,并且仅在已经进行CTS.Cancel()
调用时才会发生。您的任务代码是需要停止工作并不时检查令牌以取消的任务代码。
你似乎对取消如何运作感到困惑。 这是一些基本信息。
You won't be able to cancel the task during those 5 seconds of sleep. Cancelling will only occur at the
ThrowIfCancellationRequested()
call and only if theCTS.Cancel()
call had already been made.Your task code is the one who needs to stop work and check the token for cancellation from time to time.
You seem to be confused about how cancellation works. Here's some basic info.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
睡觉时唤醒任务(Awaken a task while sleeping)[2021-09-10]
首先,我建议你放弃Thread + Sleep + Invoke组合进行定时操作。 这很难看。 WinForms和WPF都有自定义类来自动执行这三项操作(定期从调度程序线程更新GUI)。 查看System.Windows.Forms.Timer和System.Windows.Threading.DispatcherTimer 。 现在针对您的具体问题,您可以简单地定义一个通用方法来更新GUI,并根据计时器代码和按钮处理程序调用它。 First off I'd advise you to drop the T ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
您似乎使事情变得复杂,只需使用jQuery的动画和延迟,并在单击取消按钮时停止它们 var helper = $(".helper"); var helperIndex = -1; (function showNextHelp() { ++helperIndex; helper.eq(helperIndex % helper.length) .fadeIn(500) .delay(1000) .fadeOut(500, showN ...
-
我在这里看到两个问题: 首先,正如Vincent在评论中所说的那样,您将通过AsAsyncAction().AsTask(cts.Token); 在按钮单击处理程序和.AsTask(ct); 在方法本身。 其次,更重要的是,您将取消令牌传递给任务,但您从未在方法中使用它。 将取消令牌传递给任务主要是并行使用,而不是异步工作。 这是一种协调和查询一次运行的多个任务的执行状态的方法。 无论如何,它始终依赖于令牌本身在执行代码中的使用。 您可以这样想,您告诉任务对象本身您正在取消操作,但您的代码不知道如何处理它。 ...
-
在这5秒睡眠期间,您将无法取消任务。 取消只会在ThrowIfCancellationRequested()调用时发生,并且仅在已经进行CTS.Cancel()调用时才会发生。 您的任务代码是需要停止工作并不时检查令牌以取消的任务代码。 你似乎对取消如何运作感到困惑。 这是一些基本信息。 You won't be able to cancel the task during those 5 seconds of sleep. Cancelling will only occur at the ThrowIf ...
-
如何在C#Winforms应用程序中取消长时间运行的异步任务的执行(How to cancel execution of a long-running async task in a C# Winforms app)[2023-04-14]
如果要在中途取消实际任务,则需要查看使用CancellationTokenSource并将取消令牌传递给异步方法。 这是微软的文档 ,它有一个很好的例子靠近底部, 这是另一个很好的博客 ,显示进度条并允许取消。 第二篇文章有一个很好的概述: 取消由CancellationToken结构控制。 您在可取消异步方法的签名中公开取消令牌,使它们能够在任务和调用者之间共享。 在最常见的情况下,取消遵循此流程: 调用者创建一个CancellationTokenSource对象。 调用者调用可取消的异步API,并从Ca ... -
请尝试将CancellationToken作为第二个参数传递给Task.Factory.StartNew CancellationToken ct = tokenSource.Token; Task.Factory.StartNew(() => { // do some work... if (ct.IsCancellationRequested) { // another threa ...
-
取消是合作的 ,如果您希望取消操作,则需要编辑要取消的功能。 所以执行会成为 public async Task Execute() { _cancellationTokenSource = new CancellationTokenSource(); var taskCompletionSource = new TaskCompletionSource
-
从bw.CancelAsync()我想使用BackgroundWorker 。 我还猜测上面的代码是由后台工作者执行的。 但是,在该代码中没有任何地方,我可以看到您实际上正在检查是否应该取消该过程。 此外,您在此行中一次性下载图像: Image img = Image.FromStream(stream); 这不支持取消。 所以问题是: 您无法取消所有文件的循环,因为您没有检查是否应取消后台工作程序 您无法取消单个图像下载,因为您一次性下载它 解决方案: 在循环中,添加条件,如果后台工作程序被取消,则退出 ...