公共静态hashmap清除并且放置不正常

时间:2014-08-10 07:06:07

标签: java android android-intent android-button

首先我点击“mn_column”按钮,然后正常工作。但我回去再次点击“mn_column”或“mn_blog”然后显示流动错误:

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)

照片: enter image description here

代码: 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);

  }
 }

请帮帮我。

1 个答案:

答案 0 :(得分:1)

您的解析器变量不正确。所以你 添加流动代码:

 RssParser pareser= new RssParser();

尝试这种方式:

  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);

            }