2013-04-21 75 views
0

我只是想要爲使用ListView作爲我的課程遊戲的新聞提要的活動提供框架。 在該活動中有簡單的新聞提要,和刷新按鈕(以更新的最新消息ListView控件)Android Activity Breaking

我activity_news.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_gravity="center"> 


<ListView 
    android:id="@+id/news" 
    android:layout_width="match_parent" 
    android:layout_height="320dp" 

    > 
</ListView> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="45dp" 
    android:text="Refresh" 
    android:id="@+id/bRef" 
/>  

我NewsActivty.java:

package com.example.mynews; 

import com.example.mynews.R; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

public class NewsActivity extends Activity { 
Button refresh; 
String newsfeed [] = {"latest news", "older news"}; 
Intent ref = new Intent(this, NewsActivity.class); 

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

    refresh = (Button) findViewById(R.id.bRef); 

    ListView newsStuff = (ListView) findViewById(R.id.news); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, newsfeed); 
    newsStuff.setAdapter(adapter); 

    refresh.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(ref); 
     } 
    }); 
} 

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

}

而且我manifes T:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.mynews" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.mynews.NewsActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

我一塊內置一切起片,並運行它爲我完成了一些新的東西(的佈局,陣列適配器等),活動開始突破,一旦我添加了OnClickListener(模擬器告訴我「我的新聞不幸停止」)我的刷新按鈕。我相信有一種更好的方法可以完成我想要做的事情,但對於迄今爲止我所做的任何幫助都將不勝感激。

+0

看看你的錯誤日誌,瞭解它爲什麼崩潰。您可以在terminal/cmd中編寫以下命令來查找日誌>> adb logcat *:E – Sharj 2013-04-21 21:06:29

+0

您是否嘗試刷新listView? – NickF 2013-04-21 21:07:40

+0

Sharj在logcat中沒有任何東西。 @NickF是的,基本上。但是我們打算做的是填充一個字符串數組,以便在調用活動時在列表視圖中顯示爲項目(因此爲什麼我要使用startActivity從當前活動(新聞提要)轉到同樣的活動,以便它重新填充列表視圖中的項目 – 2013-04-21 21:20:32

回答

0

您是否定義了類ref.java? 如果是,那麼你需要在清單文件中定義該活動(ref)。