从main调用类时,App force关闭

时间:2015-04-06 08:45:01

标签: android android-fragmentactivity fatal-error

该应用程序在以前的Android版本中运行良好,但在Android 5.0+中没有。我猜测向后兼容性存在一些问题。我尝试从日志中确定应用程序崩溃的原因但不能。

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    Log.d(TAG, "Clear 1");
    setContentView(R.layout.activity_dj);

    Log.d(TAG, "Clear 2");`

如您所见,类的上述代码段设置指定的布局。

activity_dj.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DJActivity" >

<fragment
    android:id="@+id/frag_dj_music"
    android:name="com.example.musicsaround.dj.DJMusicFragment"
    android:layout_width="match_parent"
    android:layout_height="300dp" />

<fragment
    android:id="@+id/frag_djs_devices"
    android:name="com.example.musicsaround.dj.ServerDeviceListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TextView
    android:id="@+id/txt_dj_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DJ Time" />

日志显示“清除1”但不显示“清除2”。请参阅下面的日志:

04-06 14:00:58.699: D/DJ Mode Activity(28278): Clear 1
04-06 14:00:58.719: E/AndroidRuntime(28278): FATAL EXCEPTION: main
04-06 14:00:58.719: E/AndroidRuntime(28278): Process: com.example.musicsaround, PID: 28278
04-06 14:00:58.719: E/AndroidRuntime(28278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.musicsaround/com.example.musicsaround.dj.DJActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.os.Looper.loop(Looper.java:155)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.app.ActivityThread.main(ActivityThread.java:5696)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at java.lang.reflect.Method.invoke(Native Method)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at java.lang.reflect.Method.invoke(Method.java:372)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
04-06 14:00:58.719: E/AndroidRuntime(28278): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:404)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.app.Activity.setContentView(Activity.java:2156)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at com.example.musicsaround.dj.DJActivity.onCreate(DJActivity.java:64)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.app.Activity.performCreate(Activity.java:5958)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
04-06 14:00:58.719: E/AndroidRuntime(28278):    ... 10 more
04-06 14:00:58.719: E/AndroidRuntime(28278): Caused by: java.lang.ClassCastException: com.example.musicsaround.dj.DJMusicFragment cannot be cast to android.support.v4.app.Fragment
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
04-06 14:00:58.719: E/AndroidRuntime(28278):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
04-06 14:00:58.719: E/AndroidRuntime(28278):    ... 20 more

编辑: DJMusicFragment类扩展了Fragment,是所有用户交互元素(如按钮及其事件)的容器类。我附上了第一行代码:

public class DJMusicFragment extends Fragment implements OnCompletionListener,
    SeekBar.OnSeekBarChangeListener
{
private String isbtnPlay = "no";

private ImageButton btnPlay;
private ImageButton btnNext;
private ImageButton btnPrevious;
private ImageButton btnPlaylist;
private ImageButton btnRepeat;
private ImageButton btnShuffle;
private SeekBar songProgressBar;
private TextView songTitleLabel;
private TextView songCurrentDurationLabel;
private TextView songTotalDurationLabel;
private ProgressDialog syncProgress;
.............

谢谢!

1 个答案:

答案 0 :(得分:0)

您的班级DJMusicFragment必须延长android.support.v4.app.Fragment 那是因为您使用了支持库。