【问题标题】:Android app suspends/crashes when add new code inside OnCreate在 OnCreate 中添加新代码时,Android 应用程序暂停/崩溃
【发布时间】:2014-04-23 16:54:54
【问题描述】:

我正在使用标签应用程序制作一个简单的新闻和视频,但是当我添加 onClickListener 以更改到另一个页面时遇到问题,在我添加了应用程序启动后暂停的代码之后,但是当我删除它时,它可以正常工作,并且可以使用任何其他方法,例如

 test.setText("etc");

我创建的方法是addListener();我在其他应用程序中尝试过它并且运行良好,但在这个应用程序中它只是让应用程序暂停。 如果有人可以帮助我,那就太好了,因为我已经被困在这里好几天了。

也对不起我的英语,希望你能理解我想说的话。

这是主要活动的代码

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

这个管理标签的 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;
    }
}

这是 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;
    }  
}

这是 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>

这是 Activity 的 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>

这些是我在 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

【问题讨论】:

  • 您必须检查您的 logcat 中的项目类和抛出异常的行号,以便准确了解错误发生的位置。
  • 您的应用是否收到 ANR(应用无响应)?布局有变化吗?
  • 这部分日志猫完全没用。查找 log cat 的位置是红色的,或者它专门为您提供错误或行号的位置。
  • 我上传了带有调试应用程序时发生的事件的 LongCat 代码。此外,当我尝试单击黑屏时,会出现 ANR 错误。谢谢大家的帮助,我真的很想知道为什么添加一个功能齐全的 setContentView 或事件一个 setText 会使应用程序像这样崩溃。

标签: java android eclipse android-fragments onclicklistener


【解决方案1】:

这一行的问题:

 setContentView(R.layout.news_content);

一旦你设置了 conentView,你就不能再用一个按钮来改变它了。

要再次使用设置内容视图,您必须打开新活动或重新打开当前活动。

编辑

在 xml 中,您的视图是 LinearLayout 的类型。

在你的代码中应该是这样的:

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

而不是这个:

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

编辑2

现在当我查看您的主要活动 xml 时, 你用错了! 您必须将视图寻呼机作为第二个视图,而不是作为根。

像这样:

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

编辑3

为什么要将点击侦听器添加到线性布局?同样在您的主要活动 xml 中,我找不到任何 textview .. 您不能引用当前活动之外的文本视图。 如果您查看您的 tab1.xml,您会发现您尝试设置的 textview 就在那里。

编辑4:

在片段中使用方法 findViewById 的示例: 按钮 b =(Button) android.findViewById (R.id.yourbutton) 确保在 View android = inflater.inflate(R.layout.tab1, container, false);

【讨论】:

  • 感谢您的回答,即使我只添加了一个 var.setText(String); 应用程序也会崩溃;在我的 addListener 方法的 onCreate 或事件中。我将上传另一个应用程序,此代码可以完美运行。
  • 嘿,看看你的 xml - 它应该是 LinearLayout 而不是 View 就像你在代码中所做的那样
  • @SacreDevekioer 效果很好,但我的主应用程序中的问题仍然存在,我真的认为问题是我必须使用 activity_main 而不是 fragment_activity 但我不知道在哪里
  • 哦,对不起,我弄错了,我在 MainActivity 评论了我的方法,因为它没有被调用,所以它工作 x_x 但问题仍然存在,尽管我现在正在学习更多
  • 但是你现在可以工作了吗?如果是的话你仍然可以接受我的回答:)
猜你喜欢
  • 1970-01-01
  • 2014-05-23
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 2015-06-12
  • 2018-06-18
  • 2017-12-22
  • 2021-02-11
相关资源
最近更新 更多