Application StartException上的ClasscastException

时间:2013-02-18 19:10:34

标签: android classcastexception

我正在尝试使用LoaderManager for API< 11。我试图通过使用Log实用程序找到错误。但是在logcat中找不到这样的记录。这是代码。应用程序只是尝试从Contacts中检索数据。 classcastexception的可能原因是转换不兼容的类型,我相信代码中没有这样的问题。任何帮助将不胜感激。

package com.example.androidcontact;

import android.net.Uri;
import android.os.Bundle;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;


public class MainActivity extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
    private SimpleCursorAdapter mAdapter;
    String tag = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(tag,"onCreated");

        mAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(),
                android.R.layout.simple_list_item_2,
                null,
                new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME},
                new int[]{android.R.id.text1, android.R.id.text2},
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        setListAdapter(mAdapter);

        getLoaderManager().initLoader(0, null, this);
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {

        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[]projection = new String[]{
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME};

        return new CursorLoader(getActivity(), uri, projection, null, null, null);      
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
        mAdapter.swapCursor(arg1);

    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        mAdapter.swapCursor(null);

    }
    }

拜托,别误会我的意思。我只是花时间研究如何获得堆栈跟踪。我仍然不知道如何获得一个在开始时发生的异常。这是我能找到的内容。

02-19 00:35:35.401: W/dalvikvm(533): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 00:35:43.417: E/AndroidRuntime(533): FATAL EXCEPTION: main
02-19 00:35:43.417: E/AndroidRuntime(533): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidcontact/com.example.androidcontact.MainActivity}: java.lang.ClassCastException: com.example.androidcontact.MainActivity
02-19 00:35:43.417: E/AndroidRuntime(533):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 00:35:43.417: E/AndroidRuntime(533):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 00:35:43.417: E/AndroidRuntime(533):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 00:35:43.417: E/AndroidRuntime(533):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 00:35:43.417: E/AndroidRuntime(533):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 00:35:43.417: E/AndroidRuntime(533):  at android.os.Looper.loop(Looper.java:123)
02-19 00:35:43.417: E/AndroidRuntime(533):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 00:35:43.417: E/AndroidRuntime(533):  at java.lang.reflect.Method.invokeNative(Native Method)
02-19 00:35:43.417: E/AndroidRuntime(533):  at java.lang.reflect.Method.invoke(Method.java:507)
02-19 00:35:43.417: E/AndroidRuntime(533):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 00:35:43.417: E/AndroidRuntime(533):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 00:35:43.417: E/AndroidRuntime(533):  at dalvik.system.NativeStart.main(Native Method)
02-19 00:35:43.417: E/AndroidRuntime(533): Caused by: java.lang.ClassCastException: com.example.androidcontact.MainActivity
02-19 00:35:43.417: E/AndroidRuntime(533):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 00:35:43.417: E/AndroidRuntime(533):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 00:35:43.417: E/AndroidRuntime(533):  ... 11 more

根据清单文件,我需要应用程序在GingerBread上运行,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidcontact"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:name="android.app.Application"
        >
        <activity
            android:name="com.example.androidcontact.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

您需要使用支持库中的Support Loader Manager

getActivity().getSupportLoaderManager().initLoader(0, null, this);

用于向后兼容。

答案 1 :(得分:0)

您当前的问题是,您正在尝试将片段作为活动启动,而这并不像您第一手资料那样有效。 改变你的课程,看起来像这样:

public class MainActivity extends Activity
{  

  @Override
  protected void onCreate(Bundle b)
  {
    super.onCreate(b);
    setContentView (R.layout.main);
  }

  public static class MyFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
    private SimpleCursorAdapter mAdapter;
    String tag = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Log.d(tag,"onCreated");

      mAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(),
                                         android.R.layout.simple_list_item_2,
                                         null,
                                         new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME},
                                         new int[]{android.R.id.text1, android.R.id.text2},
                                         CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

      setListAdapter(mAdapter);

      getActivity().getSupportLoaderManager().initLoader(0, null, this);
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {

      Uri uri = ContactsContract.Contacts.CONTENT_URI;
      String[]projection = new String[]{
        ContactsContract.Contacts._ID,
          ContactsContract.Contacts.DISPLAY_NAME};

      return new CursorLoader(getActivity(), uri, projection, null, null, null);      
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
      mAdapter.swapCursor(arg1);

    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
      mAdapter.swapCursor(null);

    }
  }
}

然后在res/layout中创建一个名为main.xml的布局文件,其中包含:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id ="@+id/my_fragment"
    android:name="com.example.androidcontact.MainActivity$MyFragment">
</fragment>

还包含@ hsigmond的建议,因为 使用支持库(上面的代码已经有了这个更改)。