2014-08-31 39 views
0

我的活動有一個列表視圖,它填充了來自我的parse.com數據庫的數據,並且列表沒有被填充沒有問題。它顯示一個名稱列表。我想將列表中所選項目的名稱存儲在一個變量中,並傳遞給另一個活動。列表被填充,但選擇錯誤崩潰應用程序。我的繼承人活動`當點擊列表視圖選項時出錯

import java.util.List; 

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import com.parse.ParseObject; 
import com.parse.ParseQuery; 
import com.parse.ParseQueryAdapter; 
import com.parse.ParseQueryAdapter.OnQueryLoadListener; 

public class ResterauntList extends ActionBarActivity { 
    String mValue; 
    ArrayAdapter<String> adapter; 
    ProgressDialog mProgressDialog; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_resteraunt_list); 

     Bundle bdl = getIntent().getExtras(); 
     mValue = bdl.getString("Value"); 
     setContentView(R.layout.activity_resteraunt_list); 

     populateList(mValue); 
    } 

    private void populateList(final String Value) { 
     ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() { 

      @Override 
      @SuppressWarnings({ "unchecked", "rawtypes" }) 
      public ParseQuery create() { 
       ParseQuery query = new ParseQuery("Restraunt"); 
       query.whereEqualTo("Location", Value); 
       return query; 
      } 
     }; 
     ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(
       this, factory); 
     adapter.setTextKey("Name"); 
     adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() { 

      public void onLoading() { 
       mProgressDialog = new ProgressDialog(ResterauntList.this); 
       mProgressDialog.setTitle(Value + " Restraunts Search"); 
       mProgressDialog.setMessage("Loading..."); 
       mProgressDialog.setIndeterminate(false); 
       mProgressDialog.show(); 
      } 

      public void onLoaded(List<ParseObject> objects, Exception e) { 
       mProgressDialog.dismiss(); 
      } 
     }); 

     final ListView listView = (ListView) findViewById(R.id.restListView); 
     listView.setAdapter(adapter); 
     listView.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       String name = (String) listView.getItemAtPosition(position); 
       Intent i = new Intent(getApplicationContext(), 
         SingleRestraunt.class); 
       i.putExtra("restName", name); 
       startActivity(i); 

      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.resteraunt_list, 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(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
}` 

我對這個活動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: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="com.example.gastronomaapp.ResterauntList" > 

    <ListView 
     android:id="@+id/restListView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" > 
    </ListView> 

</RelativeLayout> 

給我似乎不斷收到此錯誤

08-31 03:00:42.490: E/AndroidRuntime(2003): Process: com.example.gastronomaapp, PID: 2003 
08-31 03:00:42.490: E/AndroidRuntime(2003): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView 

經過許多解決方案,並試圖幾乎所有我能找到線上。絕對不知道什麼是錯的。將不勝感激 編輯 - 做了一個堆棧跟蹤,告訴我錯誤是在這一行。還挺有這樣的直覺

String name = (String) listView.getItemAtPosition(position); 
       Intent i = new Intent(getApplicationContext(), 
         SingleRestraunt.class); 
+0

看堆棧跟蹤並找到發生錯誤的行。 – 2014-08-31 07:19:45

+0

使用錯誤行進行編輯。謝謝 – newbie 2014-08-31 07:25:34

+0

請仔細看看堆棧跟蹤。您發佈的代碼不可能導致您發佈的錯誤。 – 2014-08-31 07:28:44

回答

1

的錯誤是以下行

String name = (String) listView.getItemAtPosition(position); 

您正在使用

INSEAD的

ArrayAdapter<String> adapter 

ParseQueryAdapter<ParseObject> adapter 

,所以你不能把它轉換成字符串。嘗試使用下面的代碼

ParseObject object= (ParseObject) listView.getItemAtPosition(position); 

然後檢查屬性的parseObject你putExtra需要什麼

+0

錯誤停止了,但是它打印出類似[email protected]的東西。我是否在我的分析對象處理中出錯? – newbie 2014-08-31 07:42:34

相關問題