2017-05-20 85 views
0

我在運行應用程序時無法實例化活動錯誤,我聲明瞭清單文件中的所有類,然後它也會拋出一個錯誤,請你幫我解決這個問題下面的問題是我的類和清單文件代碼。運行應用程序時無法實例化活動錯誤

WelcomeActivity.java

public class WelcomeActivity extends AppCompatActivity { 

    private ViewPager viewPager; 
    private MyViewPagerAdapter myViewPagerAdapter; 
    private LinearLayout dotsLayout; 
    private TextView[] dots; 
    private int[] layouts; 
    private Button btnSkip, btnNext; 
    private PrefManager prefManager; 

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

     // Checking for first time launch - before calling setContentView() 
     prefManager = new PrefManager(this); 
     if (!prefManager.isFirstTimeLaunch()) { 
      launchHomeScreen(); 
      finish(); 
     } 

     // Making notification bar transparent 
     if (Build.VERSION.SDK_INT >= 21) { 
      getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
     } 

     setContentView(R.layout.activity_welcome); 

     viewPager = (ViewPager) findViewById(R.id.view_pager); 
     dotsLayout = (LinearLayout) findViewById(R.id.layoutDots); 
     btnSkip = (Button) findViewById(R.id.btn_skip); 
     btnNext = (Button) findViewById(R.id.btn_next); 


     // layouts of all welcome sliders 
     // add few more layouts if you want 
     layouts = new int[]{ 
       R.layout.welcome_slide1, 
       R.layout.welcome_slide2, 
       R.layout.welcome_slide3, 
       R.layout.welcome_slide4}; 

     // adding bottom dots 
     addBottomDots(0); 

     // making notification bar transparent 
     changeStatusBarColor(); 

     myViewPagerAdapter = new MyViewPagerAdapter(); 
     viewPager.setAdapter(myViewPagerAdapter); 
     viewPager.addOnPageChangeListener(viewPagerPageChangeListener); 

     btnSkip.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       launchHomeScreen(); 
      } 
     }); 

     btnNext.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // checking for last page 
       // if last page home screen will be launched 
       int current = getItem(+1); 
       if (current < layouts.length) { 
        // move to next screen 
        viewPager.setCurrentItem(current); 
       } else { 
        launchHomeScreen(); 
       } 
      } 
     }); 
    } 

    private void addBottomDots(int currentPage) { 
     dots = new TextView[layouts.length]; 

     int[] colorsActive = getResources().getIntArray(R.array.array_dot_active); 
     int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive); 

     dotsLayout.removeAllViews(); 
     for (int i = 0; i < dots.length; i++) { 
      dots[i] = new TextView(this); 
      dots[i].setText(Html.fromHtml("&#8226;")); 
      dots[i].setTextSize(35); 
      dots[i].setTextColor(colorsInactive[currentPage]); 
      dotsLayout.addView(dots[i]); 
     } 

     if (dots.length > 0) 
      dots[currentPage].setTextColor(colorsActive[currentPage]); 
    } 

    private int getItem(int i) { 
     return viewPager.getCurrentItem() + i; 
    } 

    private void launchHomeScreen() { 
     prefManager.setFirstTimeLaunch(false); 
     startActivity(new Intent(WelcomeActivity.this, SplashActivity.class)); 
     finish(); 
    } 

    // viewpager change listener 
    ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int position) { 
      addBottomDots(position); 

      // changing the next button text 'NEXT'/'GOT IT' 
      if (position == layouts.length - 1) { 
       // last page. make button text to GOT IT 
       btnNext.setText(getString(R.string.start)); 
       btnSkip.setVisibility(View.GONE); 
      } else { 
       // still pages are left 
       btnNext.setText(getString(R.string.next)); 
       btnSkip.setVisibility(View.VISIBLE); 
      } 
     } 

     @Override 
     public void onPageScrolled(int arg0, float arg1, int arg2) { 

     } 

     @Override 
     public void onPageScrollStateChanged(int arg0) { 

     } 
    }; 

    /** 
    * Making notification bar transparent 
    */ 
    private void changeStatusBarColor() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      Window window = getWindow(); 
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
      window.setStatusBarColor(Color.TRANSPARENT); 
     } 
    } 

    /** 
    * View pager adapter 
    */ 
    public class MyViewPagerAdapter extends PagerAdapter { 
     private LayoutInflater layoutInflater; 

     public MyViewPagerAdapter() { 
     } 

     @Override 
     public Object instantiateItem(ViewGroup container, int position) { 
      layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View view = layoutInflater.inflate(layouts[position], container, false); 
      container.addView(view); 

      return view; 
     } 

     @Override 
     public int getCount() { 
      return layouts.length; 
     } 

     @Override 
     public boolean isViewFromObject(View view, Object obj) { 
      return view == obj; 
     } 


     @Override 
     public void destroyItem(ViewGroup container, int position, Object object) { 
      View view = (View) object; 
      container.removeView(view); 
     } 
    } 
} 

的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.slider.intro.introslider"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".WelcomeActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".MainActivity" android:label="@string/title_activity_welcome" android:theme="@style/AppTheme.PopupOverlay"> 
      </activity> 
     <activity android:name=".SplashActivity" android:label="@string/title_activity_welcome" android:theme="@style/AppTheme.PopupOverlay"/> 
    </application> 

</manifest> 

錯誤

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.slider.intro.introslider/com.slider.intro.introslider.WelcomeActivity}: java.lang.ClassNotFoundException: Didn't find class "com.slider.intro.introslider.WelcomeActivity" on path: DexPathList[[zip file "/data/app/com.slider.intro.introslider-1/base.apk"],nativeLibraryDirectories=[/data/app/com.slider.intro.introslider-1/lib/arm64, /vendor/lib64, /system/lib64]] 
+0

請出示出現在「com.slider.intro.introslider」包 – Rahul

+0

WelcomeActivity CALSS這也將無法正常工作,當我在課堂書面方式上面的包名,那麼它會拋出一個錯誤的包名。 –

回答

0

此錯誤顯示THA t您在Manifest中定義的活動不存在於定義的路徑中。使用「com.slider.intro.introslider」包中存在的「WelcomeActivity」類。

要檢查這個進入「WelcomeActivity」類作爲看的第一行代碼是完全一樣的,如下所示:

package com.slider.intro.introslider.WelcomeActivity; 

如果沒有,那麼你必須將你的「WelcomeActivity」級到「 com.slider.intro.introslider「包。

+0

活動WelcomeActivity –

+0

你沒有正確理解。你只是不必在你的課上寫這行,你必須將「WelcomeActivity」類複製到你的「com.slider.intro.introslider」包中。 –

+0

在文件資源管理器中打開您的代碼文件夾,找到您的課程「WelcomeActivity」,並從那裏剪下來,然後找到名爲「introslider」的文件夾,並將您之前剪切的課程粘貼到此文件夾中。 –

相關問題