App crashes when i try to set onClickListener

user3214370

I need help on solving an issue with app crashing when i try to set onClickListener on a button (btOK).

This is my 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 , the place where i set my buttons:

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

You have

setContentView(R.layout.activity_main);

activity_main.xml does not a have a button with id btOK

Your news_fragment.xml has a button.

So if you initialize button in Activity you get NullPointerException leading to crash.

Its the same for TextView and ListView.

findViewById will look for a view with the id in the current inflated layout.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

When I try to set a clickListener, app crashes

App crashes when I add an onClickListener to ImageView

App Crashes when try to set the maximumValue to the UISlider

App crashes when I try to turn on Bluetooth

App crashes when I try to logout

Android app crashes when i try to reLogin

App crashes when i try to run this page

App crashes when I try to open a Fragment

It crashes my app when I put onClickListener on a ImageView

TextView onClickListener crashes app when clicked on

My app crashes when I try to add Image Button to it

When I Try to Change the Color of SnackBar TextView App Crashes

MapFragment make app crashes at runtime when I try to setMapType

my app crashes when I try to download an image

App crashes when I try to insert data into database

App crashes when I try to edit an EditText view

App crashes when I try to read data from Cloud Firestore

Android:App crashes when i try to retrieve data from intent

My app crashes when i try to move to a particular layout

App crashes when i try to add data to my database

App crashes when generating random value inside a onClickListener

Why can I save an enum to Firestore, but it crashes the app when I try to retrieve the same?

Application crashes when i try to debug

Roslyn crashes when I try to run a script

Pythonw crashes when I try to run a file

When I try to add more than 4 images to the layout, the app crashes

App crashes when I try to launch in Android Kitkat 4.4.4 but works fine in Android 5 & 6 versions

why does my app crashes when i try adding a radio button to the radio group?

Android: When I set an OnClickListener for a ListView