0

I'm just trying to come up with a skeleton for an activity that uses a ListView as a news feed for a game my class is making. In this activity there's simply the news feed, and a Refresh button (to update the ListView with the latest news)

My 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"
/>     

My 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;
}

}

And my manifest:

<?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>

I built everything up piece by piece and ran it as I finished something new (the layout, the array adapter, etc) and the activity started to break once I added the OnClickListener (emulator tells me "mynews has unfortunately stopped") for my Refresh button. I'm sure there's a better way to accomplish what i'm trying to do, but any assistance with what I have so far would be greatly appreciated.

5
  • Look at your error log to understand why is it crashing. You can write following command in terminal/cmd to find out log >> adb logcat *:E
    – Sharjeel
    Apr 21, 2013 at 21:06
  • Are you trying to refresh the listView?
    – NickF
    Apr 21, 2013 at 21:07
  • Sharj there's nothing in the logcat. @NickF yes, essentially. however what we're planning to do is populate an array of strings to display as items in the list view when the activity is called (hence why i'm trying to use startActivity to go from the current activity (the news feed) to the same activity so that it re-populates the items in the list view.
    – Sean Evans
    Apr 21, 2013 at 21:20
  • I think you better set a new adapter at onClick event
    – NickF
    Apr 21, 2013 at 21:22
  • I will, eventually. For now I just want the activity to show two items in the list view (the "latest news" and "older news" strings) and for the Refresh button to take me from the NewsActivity, to the NewsActivity (as weird as that sounds) -- all without breaking.
    – Sean Evans
    Apr 21, 2013 at 21:28

1 Answer 1

0

Did u define a class ref.java? if yes then you need define that activity(ref) in the manifest file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.