我在设备上运行代码时遇到问题 [英] I'm having problems with running the code on my device

查看:237
本文介绍了我在设备上运行代码时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题。下面是我的类(Events.java),下面是错误。我继续在手机上运行我的代码,但继续发生错误。我不知道究竟是什么问题,我不知道如何解决它。我尝试了所有我抬起头但却不断出错的东西。请帮助我明显是一个菜鸟。

Here is my problem. Below is my class (Events.java) below it is the error. I continue to run my code on my phone but continue to get that error. I don't know what exactly the problem is and I don't know how to fix it. I tried everything I looked up but kept getting errors. Please help I'm a noob obviously.

package com.androidapp.restart;

import android.app.Fragment;
import android.app.LoaderManager;
import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View; 
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.getbase.floatingactionbutton.FloatingActionButton;


/**
 * Created by aa215995 on 3/2/2018.
 */

public class Events extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

private FloatingActionButton mAddEventButton;
private Toolbar mToolbar;
EventCursorAdapter mCursorAdapter;
EventDbHelper eventDbHelper = new EventDbHelper(getActivity());
ListView eventListView;
ProgressDialog prgDialog;



private static final int VEHICLE_LOADER = 0;

@Nullable
@Override
public ListView onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    eventListView = (ListView) inflater.inflate(R.layout.nav_events, container, false);


    mToolbar = (Toolbar) getView().findViewById(R.id.toolbar);
    ((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
    mToolbar.setTitle("Events");


    eventListView = (ListView) getView().findViewById(R.id.list);
    View emptyView = getView().findViewById(R.id.empty_view);
    eventListView.setEmptyView(emptyView);


    mCursorAdapter = new EventCursorAdapter(getActivity(), null);
    eventListView.setAdapter(mCursorAdapter);


    eventListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

            Intent intent = new Intent(view.getContext(), AddEvent.class);


            Uri currentVehicleUri = ContentUris.withAppendedId(EventContract.EventEntry.CONTENT_URI, id);

            // Set the URI on the data field of the intent
            intent.setData(currentVehicleUri);

            startActivity(intent);

        }
    });



    mAddEventButton = (FloatingActionButton) getView().findViewById(R.id.fab);

    mAddEventButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), AddEvent.class);
            startActivity(intent);

        }
    });


    getLoaderManager().initLoader(VEHICLE_LOADER, null, this);
    return eventListView;

}


@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    String[] projection = {
            EventContract.EventEntry._ID,
            EventContract.EventEntry.KEY_TITLE,
            EventContract.EventEntry.KEY_DATE,
            EventContract.EventEntry.KEY_TIME,
            EventContract.EventEntry.KEY_REPEAT,
            EventContract.EventEntry.KEY_REPEAT_NO,
            EventContract.EventEntry.KEY_REPEAT_TYPE,
            EventContract.EventEntry.KEY_ACTIVE

    };

    return new CursorLoader(getActivity(),   // Parent activity context
            EventContract.EventEntry.CONTENT_URI,   // Provider content URI to query
            projection,             // Columns to include in the resulting Cursor
            null,                   // No selection clause
            null,                   // No selection arguments
            null);                  // Default sort order

}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    mCursorAdapter.swapCursor(cursor);

}

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

}
}

这是错误

05-24 14:02:00.885 15627-15627/com.androidapp.restart E/AndroidRuntime: 
FATAL EXCEPTION: main

Process: com.androidapp.restart, PID: 15627

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast 
to android.widget.ListView

at com.androidapp.restart.Events.onCreateView(Events.java:47)

at com.androidapp.restart.Events.onCreateView(Events.java:31)

at android.app.Fragment.performCreateView(Fragment.java:2508)

at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1279)

at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2407)

at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2186)

at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2142)

at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2043)

at android.app.FragmentManagerImpl$1.run(FragmentManager.java:719)

at android.os.Handler.handleCallback(Handler.java:790)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:164)

at android.app.ActivityThread.main(ActivityThread.java:6494)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这是xml文件(events.xml)

This is the xml file (events.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.androidapp.restart.Events">

<ListView
    android:id="@+id/list"
    android:layout_below="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<RelativeLayout
    android:id="@+id/empty_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <TextView
        android:id="@+id/no_event_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="16dp"
        android:gravity="center"
        android:text="@string/no_cardetails"/>
</RelativeLayout>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="16dp"
    android:src="@drawable/ic_add_black_24dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

EDITED

现在我已将xml更改为相对布局,同样的问题,但这是我发现的另一个问题:

Now I have change the xml to a Relative Layout, same issues, but here is another issue I found:

at android.app.Fragment.performCreateView(Fragment.java:2508)

这是Fragment.java的部分:

Here is the section of the Fragment.java:

if (mFragmentManager != null) {
        writer.print(prefix); writer.print("mFragmentManager=");
        writer.println(mFragmentManager);
    }


推荐答案

如果您尝试访问您在XML中的现有ListView项目:

If you are trying to access your existing ListView item in the XML:

<ListView
android:id="@+id/list"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

您需要改为使用:

View view = inflater.inflate(R.layout.nav_events, container, false);    
eventListView = view.findViewById(R.id.list);

这篇关于我在设备上运行代码时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆