由于ClassCastException导致App崩溃:android.support.v4.view.ViewPager无法强制转换为android.support.v4.view.ViewPager

时间:2016-12-26 12:19:02

标签: android android-activity android-viewpager classcastexception android-update-app

`import android.support.v4.view.ViewPager; 
 public class Home extends Activity {
 private ViewPager mainViewPager;
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.home); 
        mainViewPager = (ViewPager) findViewById(R.id.home_vp_pager); //the log will redirect me from DDMS to this line.
} 
}
`

我的应用在通过OTA更新我的应用时遇到崩溃。这是我的错误日志

  

引起:java.lang.ClassCastException:android.support.v4.view.ViewPager无法强制转换为android.support.v4.view.ViewPager
  12-26 16:47:43.067:E / AndroidRuntime(3821):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)   12-26 16:47:43.067:E / AndroidRuntime(3821):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)   12-26 16:47:43.067:E / AndroidRuntime(3821):在android.app.ActivityThread.-wrap11(ActivityThread.java)   12-26 16:47:43.067:E / AndroidRuntime(3821):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1488)   12-26 16:47:43.067:E / AndroidRuntime(3821):在android.os.Handler.dispatchMessage(Handler.java:111)   12-26 16:47:43.067:E / AndroidRuntime(3821):在android.os.Looper.loop(Looper.java:207)   12-26 16:47:43.067:E / AndroidRuntime(3821):在android.app.ActivityThread.main(ActivityThread.java:5728)   12-26 16:47:43.067:E / AndroidRuntime(3821):at java.lang.reflect.Method.invoke(Native Method)   12-26 16:47:43.067:E / AndroidRuntime(3821):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)   12-26 16:47:43.067:E / AndroidRuntime(3821):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)   12-26 16:47:43.067:E / AndroidRuntime(3821):引起:java.lang.ClassCastException:android.support.v4.view.ViewPager无法强制转换为android.support.v4.view.ViewPager   12-26 16:47:43.067:E / AndroidRuntime(3821):at com.android.home.Home.onCreate(Home.java:445)   12-26 16:47:43.067:E / AndroidRuntime(3821):在android.app.Activity.performCreate(Activity.java:6309)   12-26 16:47:43.067:E / AndroidRuntime(3821):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)   12-26 16:47:43.067:E / AndroidRuntime(3821):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)   12-26 16:47:43.067:E / AndroidRuntime(3821):... 9更多

我正在使用

机器人支撑-v4.jar

甚至我的应用也反复显示此错误

新更新:

我正在使用

'compileSdkVersion 27
buildToolsVersion '27.0.3'

defaultconfig{
 ......
 minSdkVersion 19
    targetSdkVersion 21
 ....
 }
 .................
  dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation 'com.android.support:support-v4:27.1.1'
   }'

1 个答案:

答案 0 :(得分:0)

尝试扩展AppCompatActivity而不是Activity