当前位置: 首页 > 知识库问答 >
问题:

Android版本控制错误。片段中的ThemeWrapper错误

万俟光临
2023-03-14

嗨,我在我的应用程序中发现了这个错误。当我单击导航标题时,它崩溃了。我想在单击标题时转到profile fragment。它在KitKat工作。但是它被Lollipop和更高的糖压碎了。我犯了这个错误。

java.lang.IllegalArgumentExcoop:预期的接收器类型com.example.chitsumoe.good_friend。主要活动,但得到了android.view.ContextThemeWrapper。

这是我的主要活动代码。

package com.example.chitsumoe.good_friend;

import android.net.Uri;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,InformTrackFragment.OnFragmentInteractionListener{

Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    HomeFragment homefragment = new HomeFragment();
    android.support.v4.app.FragmentTransaction homefragmentTransaction    = getSupportFragmentManager().beginTransaction();
    homefragmentTransaction.replace(R.id.frame, homefragment);
    homefragmentTransaction.commit();

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

    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);

}

public void goToProfile(View view)
{
    Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
    ProfileFragment profileFragment = new ProfileFragment();
    android.support.v4.app.FragmentTransaction profileFragmentTransaction = getSupportFragmentManager().beginTransaction();
    profileFragmentTransaction.replace(R.id.frame, profileFragment);
    profileFragmentTransaction.commit();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}


@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_home)
    {
        HomeFragment homefragment = new HomeFragment();
        android.support.v4.app.FragmentTransaction homefragmentTransaction = getSupportFragmentManager().beginTransaction();
        homefragmentTransaction.replace(R.id.frame, homefragment);
        homefragmentTransaction.commit();
        // return true;
    }
    else if (id == R.id.nav_findfriend)
    {
        FindFriendFragment findFriendFragment=new FindFriendFragment();
        android.support.v4.app.FragmentTransaction findfriendfragmentTransaction = getSupportFragmentManager().beginTransaction();
        findfriendfragmentTransaction.replace(R.id.frame, findFriendFragment);
        findfriendfragmentTransaction.commit();
        // return true;

    } else if (id == R.id.nav_friendrequest)
    {
        FriendRequestFragment friendRequestFragment=new FriendRequestFragment();
        android.support.v4.app.FragmentTransaction friendrequestfragmentTransaction = getSupportFragmentManager().beginTransaction();
        friendrequestfragmentTransaction.replace(R.id.frame, friendRequestFragment);
        friendrequestfragmentTransaction.commit();
        //return true;
    }
    else if (id == R.id.nav_informtrack)
    {

        InformTrackFragment informtrackfragment=new InformTrackFragment();
        android.support.v4.app.FragmentTransaction informtrackfragmentTransaction=getSupportFragmentManager().beginTransaction();
        informtrackfragmentTransaction.replace(R.id.frame,informtrackfragment);
        informtrackfragmentTransaction.commit();
    }
    else if (id == R.id.nav_setting)
    {
        SettingFragment settingFragment=new SettingFragment();
        android.support.v4.app.FragmentTransaction settingfragmentTransaction = getSupportFragmentManager().beginTransaction();
        settingfragmentTransaction.replace(R.id.frame, settingFragment);
        settingfragmentTransaction.commit();

    }
    else if (id == R.id.nav_log_out) {

    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onFragmentInteraction(Uri uri) {

}
}

这是我的profileFragment代码。

public class ProfileFragment extends Fragment {

private OnFragmentInteractionListener mListener;

public ProfileFragment() {
    // Required empty public constructor
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
    return rootView;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}

Lollipop怎么了?

这是整个事故记录。

04-12 05:28:03.835 9141-9141/com.example.chitsumoe.good_friendE/Android运行时:致命例外:主要过程:com.example.chitsumoe.good_friend,PID: 9141java.lang.非法参数例外:com.example.chitsumoe.good_friend类型的预期接收器。主要活动,但得到android.view.ContextThemeWrapper在java.lang.reflect.Method.invoke(本地方法)在android.view.View$DeclaredOnClickListener.on点击(View.java:4447)在android.view.View.perform单击(View.java:5198)在android.view.查看$PerformClick.run(View.java:21147)在android.os.Handler.handle回调(Handler.java:739)在android.os.处理程序java: 95)在android. os.在android. app.ActivityThread. main(ActivityThread. java: 5417)在java. lang.反射。方法调用(本机方法)在com. android. interal. os。ZygoteInit$method odAndArgsCaller. run(ZygoteInit. java: 726)在com. android. interal. os。ZygoteInit. main(ZygoteInit. java: 616)

共有1个答案

慕嘉运
2023-03-14

对我来说,这是使用导航标题xml的子视图中的android:theme=“@style/ThemeOverlay.AppCompat.Dark”造成的。

这导致了错误

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="top"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/linearLayoutNavHeader"
        android:layout_width="match_parent"
        android:layout_height="@dimen/nav_header_height"
        android:background="@drawable/menu_header"
        android:orientation="vertical"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">
    </LinearLayout>
</LinearLayout>

改成这个问题就解决了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="top"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <LinearLayout
        android:id="@+id/linearLayoutNavHeader"
        android:layout_width="match_parent"
        android:layout_height="@dimen/nav_header_height"
        android:background="@drawable/menu_header"
        android:orientation="vertical"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin">
    </LinearLayout>
</LinearLayout>
 类似资料:
  • 我正在写一个android应用程序,其中包括一个谷歌地图片段。当我将地图片段从包含它的活动中排除时,应用程序运行良好。当我包含映射片段时,我会得到下面blockquote中引用的异常。最让人恼火的是,就在几周前,这还没有发生。这个活动运行了几个星期,然后我更新了android studio,现在我得到了这个例外。我已经在google和stackoverflow上搜索了几个解决方案,但没有任何效果。

  • 这是我的档案 当我尝试导出时,出现以下错误。我搜索并没有得到修复它的结果。

  • 更新的Android Studio。 下载的gradle。在PC的环境设置中更改了Gradle文件夹。 在Android Studio中,返回

  • 我正试着在我的电脑上用Linux Mint 64位的android studio。我安装了java-8-oracle,如下所示:错误现在运行Android Studio,当我启动一个新项目时,它会向我返回一条消息: 但目前我无法修复它...你能帮帮我吗?多谢!

  • 可能的异常 关于 FreeMarker 发生的异常,可以分为如下几类: 当配置 FreeMarker 时发生异常:典型地情况,就是在应用程序初始化时, 仅仅配置了一次 FreeMarker。在这个过程中,异常就会发生, 从 FreeMarker 的API中,我们可以很清楚的看到这一点... 当加载和解析模板时发生异常:调用了 Configuration.getTemplate(...) 方法, F

  • 我试图建立我的Android项目,但当我重建我收到这个错误消息: 现在必须显式声明注释处理器。发现编译类路径上的以下依赖项包含注释处理器。请将它们添加到annotationProcessor配置中。-黄油刀-5.1。2.jar(butterknife-5.1.2.jar) 或者,设置android。defaultConfig。javaCompileOptions。注释处理器选项。includeCo