在OnCreate中添加新代码时,Android应用会暂停/崩溃

时间:2014-04-23 16:54:55

标签: java android eclipse android-fragments onclicklistener

我正在使用标签应用制作一个简单的新闻和视频,但是当我添加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>

这是活动的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

1 个答案:

答案 0 :(得分:0)

这一行的问题:

 setContentView(R.layout.news_content);

设置conentView后,无法再使用按钮进行更改。

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

修改

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

在您的代码中,它应该是这样的:

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

而不是:

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

<强> EDIT2

现在当我查看你的主要活动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>

<强> EDIT3

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

<强> Edit4:

在片段内使用方法findViewById的示例: 按钮b =(按钮)android.findViewById(R.id.yourbutton) 确保在查看android = inflater.inflate(R.layout.tab1,container,false)后写这个;