簡體   English   中英

當我嘗試設置onClickListener時,應用程序崩潰

[英]App crashes when i try to set onClickListener

當我嘗試在按鈕(btOK)上設置onClickListener時,我需要幫助解決應用程序崩潰的問題。

這是我的MainActivity.java:

package edu.np.ece.mapg.newsweather;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import edu.np.ece.mapg.proj.fragments.TabsPagerAdapter;
import edu.np.ece.mapg.proj.rss.RssItem;
import edu.np.ece.mapg.proj.rss.RssReader;

public class MainActivity extends FragmentActivity {

ViewPager mViewPager;
TabsPagerAdapter mAdapter;
ActionBar mActionBar;
String[] tabStrings = {"Main", "News", "Weather"};
TextView tv;
Button btOK;

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

    btOK = (Button)findViewById(R.id.btOK);
    tv = (TextView)findViewById(R.id.textView1);

    btOK.setOnClickListener(asd);

    //Initialize
    mViewPager = (ViewPager)findViewById(R.id.pager);
    mActionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());  //Initialize the created adapter class

    mViewPager.setAdapter(mAdapter);
    mActionBar.setHomeButtonEnabled(false);
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    //Add tabs
    for(String tab_name : tabStrings){
        mActionBar.addTab(mActionBar.newTab().setText(tab_name).setTabListener(tabListener));
    }
    mViewPager.setOnPageChangeListener(pageListener);


    // MINI PROJ PHASE 2 //
    try{
        RssReader rssReader = new RssReader("http://news.google.com/news?pz=1&cf=all&ned=en_sg&hl=en&output=rss");
        ListView newsListView = (ListView)findViewById(R.id.newsListView);
        //Create list adapter
        ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(getBaseContext(), android.R.layout.simple_list_item_1, rssReader.getItems());
        //Set list adapter for listview
        newsListView.setAdapter(adapter);
        //Set listview item click listener
        newsListView.setOnItemClickListener((OnItemClickListener) new ListListener(rssReader.getItems(), this));
        adapter.notifyDataSetChanged();
    }
    catch(Exception e){
        Log.e("SimpleRssReader", e.getMessage());
    }
} 

View.OnClickListener asd = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        tv.setText("OK");

    }
};

ViewPager.OnPageChangeListener pageListener = new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        // On changing page, make respected tab selected
        mActionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {};

    @Override
    public void onPageScrollStateChanged(int arg0) {};
};

ActionBar.TabListener tabListener = new ActionBar.TabListener() {

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
};

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

}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

news_fragment.xml,我設置按鈕的地方:

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<Button
    android:id="@+id/btOK"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<ListView
    android:id="@+id/newsListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

你有

setContentView(R.layout.activity_main);

activity_main.xml沒有id為btOK的按鈕

您的news_fragment.xml有一個按鈕。

因此,如果在Activity中初始化按鈕,則會出現NullPointerException導致崩潰。

它與TextViewListView相同。

findViewById將在當前膨脹的布局中查找id為id的視圖。

http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

更改

View.OnClickListener asd = new View.OnClickListener()

btOK.OnClickListener(new View.OnClickListener())

同樣

SetOnPageListenermViewPagerTabListenermActionBar

buttonOk和textView1在news_fragment.xml中,您可以在設置news_fragment.xml的片段中獲取這些視圖,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM