2015-07-21 63 views
-1

這裏是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) 
+0

你甚至閱讀的錯誤信息?我認爲*「你的內容必須有一個ListView,其id屬性是'android.R.id.list'」*很清楚。你究竟是不明白的? – m0skit0

+0

如果答案有效,你爲什麼不接受它 – Soham

回答

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爲「一個ListView對象@android:ID /列表

+1

它比thanx好! –

+0

@JosephVarkey - 很高興聽到。既然我的答案確實奏效,你會介意接受嗎?謝謝 – mattfred

0

你activtiy延伸ListActivity因此,作爲崩潰日誌說:

您的內容必須具有一個ListView其id屬性爲「android.R.id.list」

這意味着,在您的XML文件,你應該與屬性的ListViewandroid:id:"android.R.id.list

這是因爲ListActivity希望在內容視圖中找到ListView

0

您正在使用的是ListActivity,與正常的Activity有點不同。

如果你什麼都不做,那麼你的ListActivity將有一個單一的視圖,它是一個ListView

如果調用setContentView()提供自己的佈局(也許你想顯示更多的不僅僅是一個ListView),那麼你的佈局必須包含ListViewListView必須@android:id/list的ID。

由於您提供的自定義佈局由RelativeLayout與單個TextView組成,因此您的佈局未通過此要求。由於您看起來只有佔位符內容,因此我會移除佈局並刪除對setContentView()的呼叫。

您從logcat中發佈的例外提到了這一點:

您的內容必須有一個ListView其id屬性爲「android.R.id.list」

相關問題