¿Por qué obtener NoSuchFiledException al usar otro proyecto android como biblioteca?
Tengo un proyecto de androide que puede funcionar por sí mismo. Quiero crear otro proyecto android que amplíe el proyecto anterior. Pero lanza NoSuchFieldException cuando el proyecto de biblioteca intenta crear algún componente que utiliza findViewById (R.id.something).
Aquí está el código para el proyecto de biblioteca:
- FindViewById dentro de un método estático
- No es necesario emitir el resultado de findViewById?
- ¿Cómo puedo "findViewById" con fragmentos?
- NullPointerException al llamar a findViewById () para obtener la vista de un fragmento
- Android: ¿Cómo usar mediaController en la clase de servicio?
/* * Copyright 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.effectivenavigation; import android.app.ActionBar; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.TextView; public class MainActivity extends FragmentActivity implements ActionBar.TabListener { /** * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the * three primary sections of the app. We use a {@link android.support.v4.app.FragmentPagerAdapter} * derivative, which will keep every loaded fragment in memory. If this becomes too memory * intensive, it may be best to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}. */ protected AppSectionsPagerAdapter mAppSectionsPagerAdapter; /** * The {@link ViewPager} that will display the three primary sections of the app, one at a * time. */ protected ViewPager mViewPager; public void onCreate(Bundle savedInstanceState) { // this.requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create the adapter that will return a fragment for each of the three primary sections // of the app. mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager()); // Set up the action bar. final ActionBar actionBar = getActionBar(); // Specify that the Home/Up button should not be enabled, since there is no hierarchical // parent. actionBar.setHomeButtonEnabled(false); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayUseLogoEnabled(false); actionBar.setDisplayShowTitleEnabled(false); closeOptionsMenu(); // Specify that we will be displaying tabs in the action bar. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Set up the ViewPager, attaching the adapter and setting up a listener for when the // user swipes between sections. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mAppSectionsPagerAdapter); mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { // When swiping between different app sections, select the corresponding tab. // We can also use ActionBar.Tab#select() to do this if we have a reference to the // Tab. actionBar.setSelectedNavigationItem(position); } }); // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) { // Create a tab with text corresponding to the page title defined by the adapter. // Also specify this Activity object, which implements the TabListener interface, as the // listener for when this tab is selected. actionBar.addTab( actionBar.newTab() .setText(mAppSectionsPagerAdapter.getPageTitle(i)) .setTabListener(this)); } } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in the ViewPager. mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public boolean onCreateOptionsMenu(Menu menu) { //this method is used for adding menu items to the Activity // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } protected void setViewPager(ViewPager viewPager) { this.mViewPager = viewPager; } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary * sections of the app. */ public static class AppSectionsPagerAdapter extends FragmentPagerAdapter { public AppSectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return new LaunchpadSectionFragment(); default: // The other sections of the app are dummy placeholders. Fragment fragment = new DummySectionFragment(); Bundle args = new Bundle(); args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); fragment.setArguments(args); return fragment; } } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { return "Section " + (position + 1); } } /** * A fragment that launches other parts of the demo application. */ public static class LaunchpadSectionFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false); // Demonstration of a collection-browsing activity. rootView.findViewById(R.id.demo_collection_button) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getActivity(), CollectionDemoActivity.class); startActivity(intent); } }); // Demonstration of navigating to external activities. rootView.findViewById(R.id.demo_external_activity) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Create an intent that asks the user to pick a photo, but using // FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, ensures that relaunching // the application from the device home screen does not return // to the external activity. Intent externalActivityIntent = new Intent(Intent.ACTION_PICK); externalActivityIntent.setType("image/*"); externalActivityIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); startActivity(externalActivityIntent); } }); return rootView; } } /** * A dummy fragment representing a section of the app, but that simply displays dummy text. */ public static class DummySectionFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false); Bundle args = getArguments(); ((TextView) rootView.findViewById(android.R.id.text1)).setText( getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER))); return rootView; } } }
Y el proyecto que lo extiende:
import com.example.android.effectivenavigation.MainActivity; import com.example.android.effectivenavigation.R; import android.os.Bundle; import android.app.ActionBar; import android.app.Activity; import android.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.Menu; public class MainActivity2 extends MainActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); } }
Y la NoSuchFieldException aparece aquí:
mViewPager = (ViewPager) findViewById(R.id.pager); java.lang.NoSuchFieldError: com.example.android.effectivenavigation.R$id.pager
Creo que lo mismo ocurre cuando se crean componentes usando findViewById () en el proyecto de biblioteca. ¿Alguna solución? Quiero mantener el proyecto infantil lo más ligero posible delegando toda la construcción en el proyecto de la biblioteca.
Así es como el proyecto de biblioteca declara el ViewPager:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" />
Citado de developer.android.com: Lanzado cuando el VM advierte que un programa intenta referenciar, en una clase o un objeto, un campo que no existe. Pero el ViewPager se declara y funciona perfectamente bien por sí mismo no como un proyecto de biblioteca.
- No se puede resolver el método 'findViewById (int)' en Fragment
- Fragmento de Android: findViewById devuelve null
- FindViewById devuelve null - Android - setContentView
- getActivity (). findViewById (R.layout.contacts_list_view) devuelve null
- Android: ¿cómo puedo acceder a las vistas (utilizando findViewById) de una actividad de un fragmento onCreateView ()?
- Cómo inflar correctamente un diseño con un ViewFlipper anidado?
- No se puede resolver el método 'findViewById (int)'
- Resultado del molde findViewById android method
La biblioteca y el proyecto secundario tienen el mismo nombre de diseño: activity_main. Renombrar cualquiera de ellos resuelve el problema.
Android, NoSuchFieldError al iniciar la segunda actividad
Asegúrese de que ha creado el widget que tiene un identificador "pager" y también asegúrese de que mViewPager no apunta a null.
- Problema con onclicklistener a LinearLayout
- GC optimization: declarando el objeto como campo opuesto a declararlo localmente