Android应用程序挂起/崩溃时添加新的code里面的OnCreate [英] Android app suspends/crashes when add new code inside OnCreate

查看:587
本文介绍了Android应用程序挂起/崩溃时添加新的code里面的OnCreate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个简单的新闻和视频标签的应用程序,但我有问题,当我加入 onClickListener 切换到另一个页面,之后我加入code将其应用程序挂起它启动后,但是当我删除它,它工作正常,它发生与任何其他方法,比如

test.setText(等);

我创建的方法是的addListener();我试图在其他应用程序,并完美地工作,但在这其中,它只是使应用程序中止。
如果有人可以帮助我将是非常好的,因为我一直以来一些日子困在这里。

也对不起我的英语水平,我希望你能明白我想说什么。

下面是主要活动的code

包com.example.link_test;进口android.app.ActionBar;
进口android.app.FragmentTransaction;
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.view.ViewPager;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.LinearLayout;
进口android.widget.TextView;公共类MainActivity扩展FragmentActivity {
  ViewPager标签;
    TabPagerAdapter TabAdapter;
  动作条动作条;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        TabAdapter =新TabPagerAdapter(getSupportFragmentManager());
        片=(ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                新ViewPager.SimpleOnPageChangeListener(){
                    @覆盖
                    公共无效使用onPageSelected(INT位置){
                      动作条= getActionBar();
                      actionBar.setSelectedNavigationItem(位置); }
                });
        Tab.setAdapter(TabAdapter);
        动作条= getActionBar();
        //启用操作栏标签
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener =新ActionBar.TabListener(){
      @覆盖
      公共无效onTabReselected(android.app.ActionBar.Tab选项卡,
          FragmentTransaction英尺){
        // TODO自动生成方法存根
      }
      @覆盖
       公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction英尺){
              Tab.setCurrentItem(tab.getPosition());
          }
      @覆盖
      公共无效onTabUnselected(android.app.ActionBar.Tab选项卡,
          FragmentTransaction英尺){
        // TODO自动生成方法存根
      }};
      //添加新标签
      actionBar.addTab(actionBar.newTab()的setText(新闻报)setTabListener(tabListener));
      actionBar.addTab(actionBar.newTab()的setText(视频)setTabListener(tabListener));
      actionBar.addTab(actionBar.newTab()的setText(TAB3)setTabListener(tabListener));
      的addListener();
    }
    公共无效的addListener(){
        最后查看TAB1 =(查看)findViewById(R.layout.tab1);
        的LinearLayout NEWLINK =(的LinearLayout)tab1.findViewById(R.id.news1);
        newLink.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                TextView的testText =(TextView中)tab1.findViewById(R.id.newstext1);
                testText.setText(这段文字已modfied);
            }
        });
   }
    }

这是管理选项卡TabAdapterPager类

 包com.example.link_test;进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentStatePagerAdapter;公共类TabPagerAdapter扩展FragmentStatePagerAdapter {
    公共TabPagerAdapter(FragmentManager FM){
    超(FM);
    // TODO自动生成构造函数存根
  }    @覆盖
    公共片段的getItem(int i)以{
        // TODO自动生成方法存根
        开关(ⅰ){
        情况下0:
            // Fragement为Android标签
            返回新TAB1();
        情况1:
           //片段对于iOS标签
            返回新TAB2();
        案例2:
            //片段的Windows选项卡
            返回新TAB3();
        }
        返回null;
    }    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回3;
    }
}

这是Tab1.java

 包com.example.link_test;进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnClickListener;
进口android.widget.LinearLayout;
进口android.widget.TextView;    公共类TAB1扩展片段{
          公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                  捆绑savedInstanceState){
              最后查看的Andr​​oid = inflater.inflate(R.layout.tab1,集装箱,FALSE);              的LinearLayout NEWLINK =(的LinearLayout)android.findViewById(R.id.news1);
              newLink.setOnClickListener(新OnClickListener(){                @覆盖
                公共无效的onClick(视图v){
                    // TODO自动生成方法存根
                    TextView的文本1 =(TextView中)android.findViewById(R.id.newstext1);
                    text1.setText(这段文字进行了修改);                }
                });              返回机器人;
    }
}

这是tab1.xml具有包含的按钮布局

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            的xmlns:工具=htt​​p://schemas.android.com/tool​​s
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_gravity =顶
            机器人:背景=#000000
            机器人:方向=垂直
            机器人:填充=5DP
            工具:上下文=$ com.example.test.test PlaceholderFragment
            >        <的TextView
            机器人:ID =@ + ID / section_label
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            >< / TextView的>
        <滚动型
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT>            <的LinearLayout
                机器人:ID =@ + ID / fragNew
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginLeft =2DP
                机器人:方向=垂直>                <! - 这是新的第一部分 - >                <的LinearLayout
                    机器人:ID =@ + ID /新闻1
                    机器人:layout_width =match_parent
                    机器人:layout_height =WRAP_CONTENT
                    机器人:方向=横向>                    < ImageView的
                        机器人:ID =@ + ID / ImageView02
                        机器人:layout_width =60dp
                        机器人:layout_height =60dp
                        机器人:layout_gravity =左
                        机器人:layout_marginRight =10dp
                        机器人:contentDescription =图像3
                        机器人:SRC =@绘制/ RSL/>                    <的LinearLayout
                        机器人:layout_width =match_parent
                        机器人:layout_height =match_parent
                        机器人:layout_gravity =中心
                        机器人:方向=垂直>                        <的TextView
                            机器人:ID =@ + ID / newstext1
                            机器人:layout_width =WRAP_CONTENT
                            机器人:layout_height =WRAP_CONTENT
                            机器人:layout_gravity =左|中心
                            机器人:layout_weight =1
                            机器人:fontFamily中=宋体
                            机器人:文字=这将是新的丝毫不差,如果太长信息技术和者,马上就...底
                            机器人:textAppearance =机器人:ATTR / textAppearanceMedium
                            机器人:文字颜色=#FFF
                            机器人:TEXTSIZE =14sp/>                        <的TextView
                            机器人:ID =@ + ID / TextView01
                            机器人:layout_width =WRAP_CONTENT
                            机器人:layout_height =WRAP_CONTENT
                            机器人:layout_gravity =左|中心|底
                            机器人:文字=日期:2014年4月31日
                            机器人:textAppearance =机器人:ATTR / textAppearanceSmall
                            机器人:文字颜色=#FFF
                            机器人:TEXTSIZE =12SP/>
                    < / LinearLayout中>
                < / LinearLayout中>
                <! - 这是第一个新的结尾 - >                <查看
                    机器人:ID =@ + ID / View01
                    机器人:layout_width =match_parent
                    机器人:layout_height =1DP
                    机器人:layout_marginBottom =5DP
                    机器人:layout_marginTop =5DP
                    机器人:背景=#FFFFFF/>
                <! - 下一个新的由视图项目分离 - >
            < /滚动型>
        < / LinearLayout中>

这是活动的XML

 < RelativeLayout的
机器人:ID =@ + ID / RelativeLayout1
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent> < android.support.v4.view.ViewPager
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID /寻呼机
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>
   < / RelativeLayout的>

这些都是我在LogCat中调试应用程序显示的行:

  7月4日至24日:47:02.618:D / AndroidRuntime 1435:>>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit<<<<<<
7月4日至24日:47:02.688:D / AndroidRuntime(1435):CheckJNI为ON
7月4日至24日:47:03.088:D / dalvikvm 1435:试图加载的lib libjavacore.so为0x0
7月4日至24日:47:03.118:D / dalvikvm 1435:新增共享库libjavacore.so为0x0
7月4日至24日:47:03.188:D / dalvikvm(1435):试图加载的lib libnativehelper.so为0x0
7月4日至24日:47:03.188:D / dalvikvm 1435:新增共享库libnativehelper.so为0x0
7月4日至24日:47:03.198:D / dalvikvm(1435):无JNI_OnLoad在libnativehelper.so为0x0发现,跳过的init
7月4日至24日:47:03.718:D / dalvikvm(1435):注:类Landroid /应用/ ActivityManagerNative;拥有179未实现(抽象)方法
7月4日至24日:47:04.518:D / LightsService(384):过多的延迟设置光:266ms
7月4日至24日:47:04.588:D / LightsService(384):过多的延迟设置光:68ms
7月4日至24日:47:04.728:D / LightsService(384):过多的延迟设置光:137ms
7月4日至24日:47:04.798:D / LightsService(384):过多的延迟设置光:68ms
7月4日至24日:47:05.498:E / memtrack(1435):无法加载memtrack模块(没有这样的文件或目录)
>七月四日至24日:47:05.498:E / android.os.Debug 1435:加载失败memtrack模块:-2
7月4日至24日:47:06.008:D / AndroidRuntime(1435):在主入口com.android.commands.pm.Pm
7月4日至24日:47:06.128:D / AndroidRuntime 1435:关闭VM
7月4日至24日:47:06.148:D / dalvikvm 1435:调试器分离;对象注册了1项
7月4日至24日:47:08.278:D / AndroidRuntime(1446):>>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit<<<<<<
7月4日至24日:47:08.298:D / AndroidRuntime(1446):CheckJNI为ON
7月4日至24日:47:08.448:D / dalvikvm(1446):试图加载的lib libjavacore.so为0x0
7月4日至24日:47:08.468:D / dalvikvm(1446):增加了共享库libjavacore.so为0x0
7月4日至24日:47:08.518:D / dalvikvm(1446):试图加载的lib libnativehelper.so为0x0
7月4日至24日:47:08.528:D / dalvikvm(1446):增加了共享库libnativehelper.so为0x0
7月4日至24日:47:08.528:D / dalvikvm(1446):无JNI_OnLoad在libnativehelper.so为0x0发现,跳过的init
7月4日至24日:47:08.928:D / dalvikvm(1446):注:类Landroid /应用/ ActivityManagerNative;拥有179未实现(抽象)方法
>七月四日至24日:47:10.008:E / memtrack(1446):无法加载memtrack模块(没有这样的文件或目录)
>七月四日至24日:47:10.008:E / android.os.Debug(1446):加载失败memtrack模块:-2
7月4日至24日:47:10.568:D / AndroidRuntime(1446):在主入口com.android.commands.am.Am
7月4日至24日:47:10.708:I / ActivityManager(384):强制停止com.example.link_test的appid = 10062用户= -1:设置调试应用程序
7月4日至24日:47:10.718:I / ActivityManager(384):杀1422:com.example.link_test / u0a62(形容词0)时,停止com.example.link_test
7月4日至24日:47:10.758:I / ActivityManager(384):强制完成活动ActivityRecord {b306fee8 U0 com.example.link_test / .MainActivity T5}
7月4日至24日:47:11.108:I / ActivityManager(384):开始U0 {行为= android.intent.action.MAIN猫= [android.intent.category.LAUNCHER] FLG = 0x10000000的CMP = com.example.link_test / .MainActivity}从PID 1446
7月4日至24日:47:11.318:D / AndroidRuntime(1446):关闭VM
7月4日至24日:47:11.368:D / dalvikvm(1446):调试器脱落;对象注册了1项
7月4日至24日:47:11.438:I /编舞(384):49跳过帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:11.808:I /编舞(1382年):跳过211帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:11.828:W / ActivityManager(384):为ActivityRecord活动暂停超时{b2d157a0 U0 com.android.launcher / com.android.launcher2.Launcher T1}
7月4日至24日:47:11.948:I / ActivityManager(384):启动PROC com.example.link_test的活动com.example.link_test / .MainActivity:PID = 1457 UID = 10062导报= {} 50062
7月4日至24日:47:11.988:D / dalvikvm(1457):不晚启用CheckJNI(已上)
7月4日至24日:47:13.508:I /编舞(384):35跳过帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:13.688:I /编舞(384):43跳过帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:13.698:W / ActivityThread(1457):应用com.example.link_test正在等待调试器端口8100 ...
7月4日至24日:47:13.718:我/的System.out(1457):发送WAIT块
7月4日至24日:47:13.828:I / dalvikvm(1457):调试器是活跃
7月4日至24日:47:13.968:I /编舞(384):跳过72帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:14.018:我/的System.out(1457):调试器连接
7月4日至24日:47:14.018:我/的System.out(1457):等待调试器来解决...
7月4日至24日:47:14.228:我/的System.out(1457):等待调试器来解决...
7月4日至24日:47:14.468:我/的System.out(1457):等待调试器来解决...
7月4日至24日:47:14.668:我/的System.out(1457):等待调试器来解决...
7月4日至24日:47:14.918:我/的System.out(1457):等待调试器来解决...
7月4日至24日:47:15.048:I /编舞(384):跳过60帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:15.168:我/的System.out(1457):等待调试器来解决...
7月4日至24日:47:15.368:我/的System.out(1457):等待调试器来解决...
7月4日至24日:47:15.488:I /编舞(384):41跳过帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:15.568:我/的System.out(1457):等待调试器来解决...
7月4日至24日:47:15.638:I /编舞(384):35跳过帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:15.768:我/的System.out(1457):等待调试器来解决...
7月4日至24日:47:15.998:我/的System.out(1457):等待调试器来解决...
7月4日至24日:47:16.348:我/的System.out(1457):调试先后落户(1382年)
7月4日至24日:47:16.558:I /编舞(384):61跳过帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:16.958:I /编舞(384):37跳过帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:17.118:I /编舞(384):38跳过帧!该应用程序可能会做它的主线程的工作太多了。
7月4日至24日:47:17.888:D / dalvikvm(1382年):GC_FOR_ALLOC释放84.5,15%的可用5705K / 6652K,暂停2424ms,总2433ms
7月4日至24日:47:18.108:D / dalvikvm(1457):GC_FOR_ALLOC释放52K,5%的游离2953K / 3080K,暂停58ms,64ms的总
7月4日至24日:47:18.108:I / dalvikvm堆(1457):成长堆(frag的情况下),以3.558MB为635812字节分配
7月4日至24日:47:18.238:D / dalvikvm(1457):GC_FOR_ALLOC释放2K,4%免​​费3571K / 3704K,暂停66ms,总66ms
7月4日至24日:47:21.288:W / ActivityManager(384):启动超时已过期,放弃唤醒锁!
>七月四日至24日:47:21.438:E /窗口管理器(384):启动窗口AppWindowToken {b2f7e518令牌=令牌{b30ee228 ActivityRecord {b2fcdcf0 U0 com.example.link_test / .MainActivity T6}}}超时


解决方案

在此行中的问题:

 的setContentView(R.layout.news_content);

一旦您设置conentView你不能再同一个按钮来更改它。

要再次使用集内容来看,你必须打开新的活动或重新打开当前活动。

修改

在XML中的看法是的LinearLayout类型。

在您的code它应该是这样的:

 的LinearLayout NEWLINK =(的LinearLayout)findViewById(R.id.news1);

而不是这样的:

 查看NEWLINK =(查看)findViewById(R.id.news1);

EDIT2

现在,当即时寻找你的主要活动XML,
你正在使用它错了!
你必须把视图寻呼机作为第二个观点,而不是作为一个根。

是这样的:

 <?XML版本=1.0编码=UTF-8&GT?;
 < RelativeLayout的
机器人:ID =@ + ID / RelativeLayout1
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent> < android.support.v4.view.ViewPager
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID /寻呼机
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>
   < / RelativeLayout的>

EDIT3

为什么你会放一个点击监听器的线性布局?还您的主要活动的xml我找不到任何的TextView ..
你不能引用一个TextView这是您的当前活动之外。
如果您在tab1.xml看,你会发现你正在尝试设置TextView的是存在的。

Edit4:

为例,使用方法findViewById里面的片段:
按钮B =(按钮)android.findViewById(R.id.yourbutton)
确保您的Andr​​oid查看后写这篇= inflater.inflate(R.layout.tab1,集装箱,FALSE);

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

解决方案

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

这篇关于Android应用程序挂起/崩溃时添加新的code里面的OnCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆