简体   繁体   English

在 OnCreate 中添加新代码时,Android 应用程序挂起/崩溃

[英]Android app suspends/crashes when add new code inside OnCreate

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我正在使用标签应用程序做一个简单的新闻和视频,但是当我添加一个onClickListener以更改到另一个页面时遇到问题,在我添加代码后应用程序在启动后暂停,但是当我删除它时,它工作正常并且它发生在任何其他方法中

 test.setText("etc");

The method I created is addListener() ;我创建的方法是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这个管理选项卡的 TabAdapterPager 类

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这是 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"这是 tab1.xml,其布局包含“按钮”

        <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这是活动的 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:这些是我在 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.设置 conentView 后,您将无法再次使用按钮更改它。

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.在 xml 中,您的视图是 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 :编辑2

now when im looking on your main activity xml , you are using it wrong!现在,当我查看您的主要活动 xml 时,您使用它是错误的! 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 :编辑3

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.同样在您的主要活动 xml 中,我找不到任何 textview .. 您不能引用当前活动之外的 textview。 If you look in your tab1.xml you will find that the textview you are trying to set is there.如果您查看 tab1.xml,您会发现您尝试设置的 textview 就在那里。

Edit4:编辑4:

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);在片段中使用方法 findViewById 的示例: Button b =(Button) android.findViewById (R.id.yourbutton) 确保在你的 View 之后写这个 android = inflater.inflate(R.layout.tab1, container, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM