Question

I'm doing a simple news and videos with tabs app but I'm having problems when I add a onClickListener to change to another page, right after I add the code it the app suspends after it is launched, but when I remove it, it works fine and It happens with any other method like

 test.setText("etc");

The method I created is addListener(); and I tried it in other app and works perfectly but in this one it just makes the app suspend. If someone could help me would be very nice cause I've been stuck here since some days.

Also sorry for my English I hope you can understand what I'm trying to say.

Here is the code of the main activity

package com.example.link_test;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
  ViewPager Tab;
    TabPagerAdapter TabAdapter;
  ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                      actionBar = getActionBar();
                      actionBar.setSelectedNavigationItem(position);                    }
                });
        Tab.setAdapter(TabAdapter);
        actionBar = getActionBar();
        //Enable Tabs on Action Bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){
      @Override
      public void onTabReselected(android.app.ActionBar.Tab tab,
          FragmentTransaction ft) {
        // TODO Auto-generated method stub
      }
      @Override
       public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
              Tab.setCurrentItem(tab.getPosition());
          }
      @Override
      public void onTabUnselected(android.app.ActionBar.Tab tab,
          FragmentTransaction ft) {
        // TODO Auto-generated method stub
      }};
      //Add New Tab
      actionBar.addTab(actionBar.newTab().setText("News").setTabListener(tabListener));
      actionBar.addTab(actionBar.newTab().setText("Videos").setTabListener(tabListener));
      actionBar.addTab(actionBar.newTab().setText("Tab3").setTabListener(tabListener));
      addListener();
    }
    public void addListener() {
        final View tab1 = (View) findViewById(R.layout.tab1);
        LinearLayout newLink = (LinearLayout) tab1.findViewById(R.id.news1);
        newLink.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                TextView testText = (TextView) tab1.findViewById(R.id.newstext1);
                testText.setText("This text has been modfied");
            }
        });
   }
    }

This TabAdapterPager class that manages the tabs

package com.example.link_test;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class TabPagerAdapter extends FragmentStatePagerAdapter {
    public TabPagerAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
  }

    @Override
    public Fragment getItem(int i) {
        // TODO Auto-generated method stub
        switch (i) {
        case 0:
            //Fragement for Android Tab
            return new Tab1();
        case 1:
           //Fragment for Ios Tab
            return new Tab2();
        case 2:
            //Fragment for Windows Tab
            return new Tab3(); 
        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }
}

This is the Tab1.java

package com.example.link_test;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

    public class Tab1 extends Fragment {
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              final View android = inflater.inflate(R.layout.tab1, container, false);

              LinearLayout newLink = (LinearLayout) android.findViewById(R.id.news1);
              newLink.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    TextView text1 = (TextView) android.findViewById(R.id.newstext1);
                    text1.setText("This text has been modified");

                }
                });

              return android;
    }  
}

This is tab1.xml that has the layout that contains the "button"

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            android:background="#000000"
            android:orientation="vertical"
            android:padding="5dp"
            tools:context="com.example.test.test$PlaceholderFragment"
            >

        <TextView 
            android:id="@+id/section_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></TextView>
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/fragNew"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="2dp"
                android:orientation="vertical" >

                <!-- This is the 1st part of the new -->

                <LinearLayout
                    android:id="@+id/news1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <ImageView
                        android:id="@+id/ImageView02"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_gravity="left"
                        android:layout_marginRight="10dp"
                        android:contentDescription="image3"
                        android:src="@drawable/rsl" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/newstext1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="left|center"
                            android:layout_weight="1"
                            android:fontFamily="tahoma"
                            android:text="This will be the tittle of the new, if is too long  it&apos;ll be ... at the end"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="#fff"
                            android:textSize="14sp" />

                        <TextView
                            android:id="@+id/TextView01"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="left|center|bottom"
                            android:text="Date: 4/31/2014"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:textColor="#fff"
                            android:textSize="12sp" />
                    </LinearLayout>
                </LinearLayout>
                <!-- This is the end of the 1st new -->

                <View
                    android:id="@+id/View01"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginBottom="5dp"
                    android:layout_marginTop="5dp"
                    android:background="#ffffff" />
                <!-- The next new separated by a view item -->


            </ScrollView>
        </LinearLayout>

This is Activity's XML

<RelativeLayout
android:id="@+id/RelativeLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

 <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" />
   </RelativeLayout>

These are all the lines that appear when I debug the app at LogCat:

04-24 07:47:02.618: D/AndroidRuntime(1435): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
04-24 07:47:02.688: D/AndroidRuntime(1435): CheckJNI is ON
04-24 07:47:03.088: D/dalvikvm(1435): Trying to load lib libjavacore.so 0x0
04-24 07:47:03.118: D/dalvikvm(1435): Added shared lib libjavacore.so 0x0
04-24 07:47:03.188: D/dalvikvm(1435): Trying to load lib libnativehelper.so 0x0
04-24 07:47:03.188: D/dalvikvm(1435): Added shared lib libnativehelper.so 0x0
04-24 07:47:03.198: D/dalvikvm(1435): No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
04-24 07:47:03.718: D/dalvikvm(1435): Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
04-24 07:47:04.518: D/LightsService(384): Excessive delay setting light: 266ms
04-24 07:47:04.588: D/LightsService(384): Excessive delay setting light: 68ms
04-24 07:47:04.728: D/LightsService(384): Excessive delay setting light: 137ms
04-24 07:47:04.798: D/LightsService(384): Excessive delay setting light: 68ms
04-24 07:47:05.498: E/memtrack(1435): Couldn't load memtrack module (No such file or directory)
>04-24 07:47:05.498: E/android.os.Debug(1435): failed to load memtrack module: -2
04-24 07:47:06.008: D/AndroidRuntime(1435): Calling main entry com.android.commands.pm.Pm
04-24 07:47:06.128: D/AndroidRuntime(1435): Shutting down VM
04-24 07:47:06.148: D/dalvikvm(1435): Debugger has detached; object registry had 1 entries
04-24 07:47:08.278: D/AndroidRuntime(1446): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
04-24 07:47:08.298: D/AndroidRuntime(1446): CheckJNI is ON
04-24 07:47:08.448: D/dalvikvm(1446): Trying to load lib libjavacore.so 0x0
04-24 07:47:08.468: D/dalvikvm(1446): Added shared lib libjavacore.so 0x0
04-24 07:47:08.518: D/dalvikvm(1446): Trying to load lib libnativehelper.so 0x0
04-24 07:47:08.528: D/dalvikvm(1446): Added shared lib libnativehelper.so 0x0
04-24 07:47:08.528: D/dalvikvm(1446): No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
04-24 07:47:08.928: D/dalvikvm(1446): Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
>04-24 07:47:10.008: E/memtrack(1446): Couldn't load memtrack module (No such file or directory)
>04-24 07:47:10.008: E/android.os.Debug(1446): failed to load memtrack module: -2
04-24 07:47:10.568: D/AndroidRuntime(1446): Calling main entry com.android.commands.am.Am
04-24 07:47:10.708: I/ActivityManager(384): Force stopping com.example.link_test appid=10062 user=-1: set debug app
04-24 07:47:10.718: I/ActivityManager(384): Killing 1422:com.example.link_test/u0a62 (adj 0): stop com.example.link_test
04-24 07:47:10.758: I/ActivityManager(384):   Force finishing activity ActivityRecord{b306fee8 u0 com.example.link_test/.MainActivity t5}
04-24 07:47:11.108: I/ActivityManager(384): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.link_test/.MainActivity} from pid 1446
04-24 07:47:11.318: D/AndroidRuntime(1446): Shutting down VM
04-24 07:47:11.368: D/dalvikvm(1446): Debugger has detached; object registry had 1 entries
04-24 07:47:11.438: I/Choreographer(384): Skipped 49 frames!  The application may be doing too much work on its main thread.
04-24 07:47:11.808: I/Choreographer(1382): Skipped 211 frames!  The application may be doing too much work on its main thread.
04-24 07:47:11.828: W/ActivityManager(384): Activity pause timeout for ActivityRecord{b2d157a0 u0 com.android.launcher/com.android.launcher2.Launcher t1}
04-24 07:47:11.948: I/ActivityManager(384): Start proc com.example.link_test for activity com.example.link_test/.MainActivity: pid=1457 uid=10062 gids={50062}
04-24 07:47:11.988: D/dalvikvm(1457): Not late-enabling CheckJNI (already on)
04-24 07:47:13.508: I/Choreographer(384): Skipped 35 frames!  The application may be doing too much work on its main thread.
04-24 07:47:13.688: I/Choreographer(384): Skipped 43 frames!  The application may be doing too much work on its main thread.
04-24 07:47:13.698: W/ActivityThread(1457): Application com.example.link_test is waiting for the debugger on port 8100...
04-24 07:47:13.718: I/System.out(1457): Sending WAIT chunk
04-24 07:47:13.828: I/dalvikvm(1457): Debugger is active
04-24 07:47:13.968: I/Choreographer(384): Skipped 72 frames!  The application may be doing too much work on its main thread.
04-24 07:47:14.018: I/System.out(1457): Debugger has connected
04-24 07:47:14.018: I/System.out(1457): waiting for debugger to settle...
04-24 07:47:14.228: I/System.out(1457): waiting for debugger to settle...
04-24 07:47:14.468: I/System.out(1457): waiting for debugger to settle...
04-24 07:47:14.668: I/System.out(1457): waiting for debugger to settle...
04-24 07:47:14.918: I/System.out(1457): waiting for debugger to settle...
04-24 07:47:15.048: I/Choreographer(384): Skipped 60 frames!  The application may be doing too much work on its main thread.
04-24 07:47:15.168: I/System.out(1457): waiting for debugger to settle...
04-24 07:47:15.368: I/System.out(1457): waiting for debugger to settle...
04-24 07:47:15.488: I/Choreographer(384): Skipped 41 frames!  The application may be doing too much work on its main thread.
04-24 07:47:15.568: I/System.out(1457): waiting for debugger to settle...
04-24 07:47:15.638: I/Choreographer(384): Skipped 35 frames!  The application may be doing too much work on its main thread.
04-24 07:47:15.768: I/System.out(1457): waiting for debugger to settle...
04-24 07:47:15.998: I/System.out(1457): waiting for debugger to settle...
04-24 07:47:16.348: I/System.out(1457): debugger has settled (1382)
04-24 07:47:16.558: I/Choreographer(384): Skipped 61 frames!  The application may be doing too much work on its main thread.
04-24 07:47:16.958: I/Choreographer(384): Skipped 37 frames!  The application may be doing too much work on its main thread.
04-24 07:47:17.118: I/Choreographer(384): Skipped 38 frames!  The application may be doing too much work on its main thread.
04-24 07:47:17.888: D/dalvikvm(1382): GC_FOR_ALLOC freed 845K, 15% free 5705K/6652K, paused 2424ms, total 2433ms
04-24 07:47:18.108: D/dalvikvm(1457): GC_FOR_ALLOC freed 52K, 5% free 2953K/3080K, paused 58ms, total 64ms
04-24 07:47:18.108: I/dalvikvm-heap(1457): Grow heap (frag case) to 3.558MB for 635812-byte allocation
04-24 07:47:18.238: D/dalvikvm(1457): GC_FOR_ALLOC freed 2K, 4% free 3571K/3704K, paused 66ms, total 66ms
04-24 07:47:21.288: W/ActivityManager(384): Launch timeout has expired, giving up wake lock!
>04-24 07:47:21.438: E/WindowManager(384): Starting window AppWindowToken{b2f7e518 token=Token{b30ee228 ActivityRecord{b2fcdcf0 u0 com.example.link_test/.MainActivity t6}}} timed out
Was it helpful?

Solution

The problem in this line:

 setContentView(R.layout.news_content);

Once you set conentView you cannot change it again with a button.

To use set content view again, you must open new activity or reopen the current activity.

Edit:

in xml your view is type of LinearLayout.

In your code its should be like this:

LinearLayout newLink = (LinearLayout) findViewById(R.id.news1);

instead of this:

View newLink = (View) findViewById(R.id.news1);

Edit2:

now when im looking on your main activity xml , you are using it wrong! You must put the view pager as a second view and not as a root.

like this:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
android:id="@+id/RelativeLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

 <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" />
   </RelativeLayout>

Edit3:

Why would you put a click listener to a linear layout? also in your main activity xml i cannot find any textview.. you cannot refer to a textview which is outside of your current activity. If you look in your tab1.xml you will find that the textview you are trying to set is there.

Edit4:

Example for using method findViewById inside fragment: Button b =(Button) android.findViewById (R.id.yourbutton) Make sure to write this after your View android = inflater.inflate(R.layout.tab1, container, false);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top