当进入Editfriends活动时,应用程序崩溃

时间:2015-07-21 14:57:26

标签: android

这是EditFriends活动..........................................

    package com.josephvarkey996gmail.test1;

import android.app.ListActivity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ArrayAdapter;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.List;


public class Editfriends extends ListActivity {
public  static final String Tag=Editfriends.class.getSimpleName();
    protected List<ParseUser> mUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_editfriends);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setProgressBarIndeterminateVisibility(true);
        ParseQuery<ParseUser> query= ParseUser.getQuery();
        query.orderByAscending(ParseConstants.key_Username);
        query.setLimit(1000);
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> users, ParseException e) {
                setProgressBarIndeterminateVisibility(true);
                if (e== null){
                      mUser=users;
                    String[] username = new String[mUser.size()];
                    int i=0;
                    for(ParseUser user:mUser){
                        username[i]=user.getUsername();
                        i++;
                    }
                   ArrayAdapter<String> adapter = new ArrayAdapter<>(Editfriends.this,android.R.layout.simple_list_item_checked,username);
                    setListAdapter(adapter);
                }
                else
                {
                    Log.e(Tag,e.getMessage());
                    AlertDialog.Builder builder=new AlertDialog.Builder(Editfriends.this);
                    builder.setMessage(e.getMessage()).setTitle(R.string.error_title).setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_editfriends, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这里是Editfriends的xml ......................................... ...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.josephvarkey996gmail.test1.Editfriends">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
我正在学习,所以无法理解这个恐怖。这是logcat ..............................

07-21 19:42:05.397    9188-9188/com.josephvarkey996gmail.test1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.josephvarkey996gmail.test1, PID: 9188
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.josephvarkey996gmail.test1/com.josephvarkey996gmail.test1.Editfriends}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ListActivity.onContentChanged(ListActivity.java:243)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:382)
            at android.app.Activity.setContentView(Activity.java:2145)
            at com.josephvarkey996gmail.test1.Editfriends.onCreate(Editfriends.java:27)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

3 个答案:

答案 0 :(得分:0)

您的主要活动类正在扩展ListActivity。这意味着您的xml文件需要ListView

尝试在XML中添加这样的内容:

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

可以在http://developer.android.com/reference/android/app/ListActivity.html找到更多信息。

正如文件所述:

  

ListActivity具有默认布局,该布局由屏幕中央的单个全屏列表组成。但是,如果需要,可以通过在onCreate()中使用setContentView()设置自己的视图布局来自定义屏幕布局。为此,您自己的视图必须包含一个ID为“@android:id / list

的ListView对象

答案 1 :(得分:0)

您的活动延长了ListActivity。因此崩溃日志说:

  

您的内容必须包含一个ListView,其id属性为&#39; android.R.id.list&#39;

这意味着您的xml文件中应该有一个ListView,其属性为android:id:"android.R.id.list

这是因为ListActivity期望在内容视图中找到ListView

答案 2 :(得分:0)

您使用的是ListActivity,与普通Activity略有不同。

如果您什么都不做,那么您的ListActivity将包含一个视图,即ListView

如果您致电setContentView()提供自己的布局(可能您希望显示的不仅仅是ListView),那么您的布局必须包含{{1} ListView 必须的ID为ListView

由于您提供的自定义布局由@android:id/list和单个RelativeLayout组成,因此您的布局不会通过此要求。由于您看起来只有占位符内容,我会删除布局并移除对TextView的调用。

您从logcat发布的异常提及:

  

您的内容必须包含一个ListView,其id属性为&#39; android.R.id.list&#39;