问题
At first I click the “mn_column” button, then works properly. But I go back and click again “mn_column” or “mn_blog” then it show flowing error :
08-10 02:30:16.441: E/AndroidRuntime(1098): FATAL EXCEPTION: main
08-10 02:30:16.441: E/AndroidRuntime(1098): Process: com.sams.main.news, PID: 1098
08-10 02:30:16.441: E/AndroidRuntime(1098): java.lang.NullPointerException
08-10 02:30:16.441: E/AndroidRuntime(1098): at com.sams.main.news.MainNewsActivity$6.onClick(MainNewsActivity.java:539)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.view.View.performClick(View.java:4438)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.view.View$PerformClick.run(View.java:18422)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.os.Handler.handleCallback(Handler.java:733)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.os.Handler.dispatchMessage(Handler.java:95)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.os.Looper.loop(Looper.java:136)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-10 02:30:16.441: E/AndroidRuntime(1098): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 02:30:16.441: E/AndroidRuntime(1098): at java.lang.reflect.Method.invoke(Method.java:515)
08-10 02:30:16.441: E/AndroidRuntime(1098): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-10 02:30:16.441: E/AndroidRuntime(1098): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-10 02:30:16.441: E/AndroidRuntime(1098): at dalvik.system.NativeStart.main(Native Method)
Picture:
Code: MainNewsActivity.java
public class MainNewsActivity extends TabActivity {
String menuLink;
int menuKey;
private List<RssItem> menuItems = new ArrayList<RssItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Button mnColumn = (Button) layout.findViewById(R.id.mn_column);
mnColumn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
menuKey = 0;
//Varialbes.menuNewsMap.clear();
menuLink = "http://www.aaa.org/rss/rssblog";
menuItems = pareser.getNewsList(menuLink);
Varialbes.menuNewsMap.put(menuKey, menuItems);
Intent intent = new Intent(MainNewsActivity.this,MenuNewsActivity.class);
intent.putExtra(Varialbes.PAPER, paper);
intent.putExtra(Varialbes.MENU_TITLE, "Column");
intent.putExtra(Varialbes.MENU_KEY, menuKey);
startActivity(intent);
}
});
Button mnBlog = (Button) layout.findViewById(R.id.mn_blog);
mnBlog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
menuKey = 1;
menuLink = "http://www.bbb.org/rss/rssblog";
menuItems = pareser.getNewsList(menuLink);
Varialbes.menuNewsMap.put(menuKey, menuItems);
Intent intent = new Intent(MainNewsActivity.this,MenuNewsActivity.class);
intent.putExtra(Varialbes.PAPER, paper);
intent.putExtra(Varialbes.MENU_TITLE, "Blog");
intent.putExtra(Varialbes.MENU_KEY, menuKey);
startActivity(intent);
}
});
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DEE4DF"
android:orientation="vertical" >
<Button
android:id="@+id/mn_column"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.98"
android:background="@drawable/menu_selector"
android:gravity="left|center_vertical|center_horizontal"
android:text="Column" />
<Button
android:id="@+id/mn_blog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.98"
android:background="@drawable/menu_selector"
android:gravity="left|center_vertical|center_horizontal"
android:text="Blog" />
</LinearLayout>
Varialbes.java
public class Varialbes {
public static final String PAPER = "paper";
public static final String MENU_TITLE = "title";
public static final String MENU_KEY = "menu_key";
public static HashMap<Integer, List<RssItem>> menuNewsMap = new HashMap<Integer, List<RssItem>>();
}
MenuNewsActivity.java
public class MenuNewsActivity extends Activity {
private ListView listNews;
private List<RssItem> items;
private Button backButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_news);
// ..........................Back..........................
backButton = (Button) findViewById(R.id.back_button);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent backIntent = new Intent(MenuNewsActivity.this,
MainNewsActivity.class);
finish();
startActivity(backIntent);
}
});
// ................................................
listNews = (ListView) findViewById(R.id.newsList);
Bundle bundle = getIntent().getExtras();
int paper = bundle.getInt(Varialbes.PAPER);
TextView tv = (TextView) findViewById(R.id.hedderText);
String menutitle = bundle.getString(Varialbes.MENU_TITLE);
int menuKey = bundle.getInt(Varialbes.MENU_KEY);
tv.setText(menutitle);
items = Varialbes.menuNewsMap.get(menuKey);
MenuNewsAdater adapter = new MenuNewsAdater(this,
Varialbes.ICONS[paper], items);
listNews.setAdapter(adapter);
}
}
Please help me.
回答1:
You pareser variable does not properly. So you
add flowing code:
RssParser pareser= new RssParser();
Try this way:
public void onClick(View v) {
menuKey = 0;
Varialbes.menuNewsMap.clear();
menuLink = "http://www.aaa.org/rss/rssblog";
RssParser pareser= new RssParser();
menuItems = pareser.getNewsList(menuLink);
Varialbes.menuNewsMap.put(menuKey, menuItems);
Intent intent = new Intent(MainNewsActivity.this,MenuNewsActivity.class);
intent.putExtra(Varialbes.PAPER, paper);
intent.putExtra(Varialbes.MENU_TITLE, "Column");
intent.putExtra(Varialbes.MENU_KEY, menuKey);
startActivity(intent);
}
来源:https://stackoverflow.com/questions/25226336/public-static-hashmap-clear-and-put-not-work-properly