大佬教程收集整理的这篇文章主要介绍了为什么在使用另一个android项目作为库时获得NoSuchFiledException?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是库项目的代码:
/* * 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,Bundle savedInstanceStatE) { View rootView = inflater.inflate(R.layout.fragment_section_dummy,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; } } }
该项目扩展了它:
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); } }
并且NoSuchFieldException出现在这里:
@H_996_9@mViewPager = (ViewPager) findViewById(R.id.pager); java.lang.NoSuchFieldError: com.example.android.effectivenavigation.R$id.pager
我相信在库项目中使用findViewById()创建组件时会发生同样的事情.
有解决方案吗我想通过将所有构造委托给库项目来尽可能地保持子项目.
这是库项目声明ViewPager的方式:
<android.support.v4.view.ViewPager xmlns:android="http://scheR_692_11845@as.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" />
引用自developer.android.com:Thrown when the VM notices that a program tries to reference,on a class or object,a field that does not exist.
但是ViewPager已经被声明并且本身完美地运行而不是作为库项目.
以上是大佬教程为你收集整理的为什么在使用另一个android项目作为库时获得NoSuchFiledException?全部内容,希望文章能够帮你解决为什么在使用另一个android项目作为库时获得NoSuchFiledException?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。