当我尝试在按钮上设置onClickListener时,我需要帮助解决应用程序崩溃的问题(btOK)。
这是我的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>
答案 0 :(得分:2)
你有
setContentView(R.layout.activity_main);
activity_main.xml
没有标识为btOK
您的news_fragment.xml
有一个按钮。
因此,如果您在Activity中初始化按钮,则会导致NullPointerException
崩溃。
TextView
和ListView
的相同内容。
findViewById
会查找当前虚增布局中包含id的视图。
http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
答案 1 :(得分:0)
更改
View.OnClickListener asd = new View.OnClickListener()
到
btOK.OnClickListener(new View.OnClickListener())
同样
SetOnPageListener
至mViewPager
和TabListener
至mActionBar
答案 2 :(得分:0)
buttonOk和textView1位于news_fragment.xml中,您可以在设置news_fragment.xml的片段中获取这些视图,