1

我已经有这个运行时错误将近一周了,但找不到解决方案(我是一个新的 android 程序员)。请帮我!

日志位于此消息的末尾。我知道我有一些类似的问题,但我找不到解决问题的方法。

我试图从活动中调用一个类,Intent但运行时错误说找不到这个类。

这是日志文件:

07-01 13:22:57.098: E/dalvikvm(1488): Could not find class 'com.XXX.ui.ViewPager',
referenced from method com.XXX.ui.MainIWrapper.onCreate
07-01 13:22:57.338: E/AndroidRuntime(1488): FATAL EXCEPTION: main
07-01 13:22:57.338: E/AndroidRuntime(1488): java.lang.RuntimeException: Unable to start
    activity ComponentInfo{com.XXX/com.XXX.ui.MainIWrapper}:

android.view.InflateException: Binary XML file line #43: Error inflating class
com.XXX.ui.NavigationBar
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.os.Handler.dispatchMessage(Handler.java:99)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.os.Looper.loop(Looper.java:130)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.app.ActivityThread.main(ActivityThread.java:3683)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
java.lang.reflect.Method.invokeNative(Native Method)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
java.lang.reflect.Method.invoke(Method.java:507)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
dalvik.system.NativeStart.main(Native
Method)
07-01 13:22:57.338: E/AndroidRuntime(1488): Caused by: android.view.InflateException:
Binary
XML file line #43: Error inflating class com.XXX.ui.NavigationBar
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at  
android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.view.LayoutInflater.inflate(LayoutInflater.java:408)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.app.Activity.setContentView(Activity.java:1657)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
com.XXX.ui.MainIWrapper.onCreate(MainIWrapper.java:69)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-01 13:22:57.338: E/AndroidRuntime(1488):     ... 11 more
07-01 13:22:57.338: E/AndroidRuntime(1488): Caused by: java.lang.ClassNotFoundException:
com.XXX.ui.NavigationBar in loader dalvik.system.PathClassLoader[/data/app/com.XXX-
2.apk]
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
java.lang.ClassLoader.loadClass(ClassLoader.java:551)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
java.lang.ClassLoader.loadClass(ClassLoader.java:511)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.view.LayoutInflater.createView(LayoutInflater.java:471)
07-01 13:22:57.338: E/AndroidRuntime(1488):     at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
07-01 13:22:57.338: E/AndroidRuntime(1488):     ... 23 more

这是处理意图的函数:

private Activity from;
private Class to;

public void open() {
    Intent intent;
    intent = new Intent(from, to);

    if (extras != null) {
        intent.putExtras(extras);
    }

    if (to.equals(MainWrapper.class)) {
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }

    if (activityForResultRequestCode == -1) {
        from.startActivity(intent);
    } else {
        from.startActivityForResult(intent, activityForResultRequestCode);
    }

    if (pendingAnimation != null && pendingAnimation.length == 2) {
        from.overridePendingTransition(pendingAnimation[0], pendingAnimation[1]);
    } else {
        from.overridePendingTransition(R.anim.slide_in_right, R.anim.hold);
    }

    if (toClosePreviousActivity) {
        from.finish();
    }   
}

这是来自活动的调用(需要更改),以及我要加载的类(它使用open()正在执行意图的函数):

UiUtils.getOpenActivityBuilder(SplashScreen.this,MainIWrapper.class).toClosePrev(true).open;

这是我清单的一部分——它是需要更改为类的主要活动MainIWrapper

<activity
    android:name=".ui.SplashScreen"
    android:configChanges="orientation"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
 </activity>

这是清单文件中的 mainWrapper 声明:

<activity
    android:name=".ui.MainIWrapper"
    android:configChanges="orientation"
    android:launchMode="singleTask"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.NoTitleBar" />

这是mainWrapper声明:

public class MainIWrapper extends BaseWebSiteScreen implements HasBox {...}

这是主要的活动声明:

public class SplashScreen extends Activity {...}

谢谢你的帮助但我仍然找不到问题(我真的是android的初学者)

这些是我的 XML 中的行(从第 43 行开始):

<com.XXX.ui.NavigationBar
        xmlns:vwpgind="http://schemas.android.com/apk/res/com.XXX"
        android:id="@+id/navigation_bar"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="top"
        vwpgind:arrowImageSrc="@drawable/common_slider_arrow"
        vwpgind:hideFirstItem="true" />

MainIWrapper.OnCreate() 的代码是:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_iwrapper);

isUserLoggedIn = UserDetails.getInstance(getApplicationContext()).isUserLoggedIn();

//we want to refresh user thumb each time user opens Main iWrapper
if (UserDetails.getInstance(getApplicationContext()).isUserLoggedIn())
UserDetails.deleteCachedUserThumb(getApplicationContext());

textBox = (TextBox) findViewById(R.id.text_box);
viewPager = (ViewPager) findViewById(R.id.view_pager);

pageViewAdapter = new PageViewAdapter();
viewPager.setAdapter(pageViewAdapter);

viewPager.setOffscreenPageLimit(NUMBER_OF_PAGES_TO_PRELOAD);

navigationBar = (NavigationBar) findViewById(R.id.navigation_bar);
topBar = (TopBar) findViewById(R.id.top_bar);
progressAnimation = (ImageView) findViewById(R.id.progress_animation);
bottomBar = (BottomBar) findViewById(R.id.bottom_bar);

}

我仍然找不到这里的错误。我查看了包含 com.XXX.ui 包的构建路径和 src 文件夹(此包包含 NavigationBar 和 ViewPager 类)


这些是我的 XML 中的行(从第 43 行开始):

 <com.XXX.ui.NavigationBar
            xmlns:vwpgind="http://schemas.android.com/apk/res/com.XXX"
            android:id="@+id/navigation_bar"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="top"
            vwpgind:arrowImageSrc="@drawable/common_slider_arrow"
            vwpgind:hideFirstItem="true" />

MainIWrapper.OnCreate() 的代码是:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_iwrapper);

isUserLoggedIn = UserDetails.getInstance(getApplicationContext()).isUserLoggedIn();

//we want to refresh user thumb each time user opens Main iWrapper
if (UserDetails.getInstance(getApplicationContext()).isUserLoggedIn())
    UserDetails.deleteCachedUserThumb(getApplicationContext());

textBox = (TextBox) findViewById(R.id.text_box);
viewPager = (ViewPager) findViewById(R.id.view_pager);

pageViewAdapter = new PageViewAdapter();
viewPager.setAdapter(pageViewAdapter);

viewPager.setOffscreenPageLimit(NUMBER_OF_PAGES_TO_PRELOAD);

navigationBar = (NavigationBar) findViewById(R.id.navigation_bar);
topBar = (TopBar) findViewById(R.id.top_bar);
progressAnimation = (ImageView) findViewById(R.id.progress_animation);
bottomBar = (BottomBar) findViewById(R.id.bottom_bar);

}

我仍然看不到这里有什么问题

public class ViewPager extends android.support.v4.view.ViewPager {

public ViewPager(Context context) {
    super(context);
}

public ViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public Parcelable onSaveInstanceState() {
    try {
        return super.onSaveInstanceState();
    } catch (Exception e) {
        return null;
    }
}
4

2 回答 2

2

编译器不会检查布局 XML 文件中引用的类是否存在,所以只在运行时报错是正常的。

很可能是com。XXX.ui.NavigationBar您的类路径中不存在类。检查你的包名,也许这只是一个错字,或者你可能在NavigationBar类的包名重构之后忘记更改 XML 文件中的包名。

于 2012-07-02T11:23:40.007 回答
0

错误都在这里:

07-01 13:22:57.098: E/dalvikvm(1488): Could not find class 'com.XXX.ui.ViewPager', referenced from method com.XXX.ui.MainIWrapper.onCreate android.view.InflateException: Binary XML file line #43: Error inflating class com.XXX.ui.NavigationBar

发布 MainIWrapper.onCreate() 中的代码,并查看导航栏的 XML 中的第 43 行。

于 2012-07-01T12:03:59.410 回答