不幸的是,appname已停止线程错误

时间:2015-06-20 19:26:34

标签: java android eclipse multithreading

我正在尝试创建一个启动mainActivity的简单android程序,当用户点击按钮时,secondActivity打开并显示一个带有三个单选按钮的无线电组。当用户点击单选按钮并按下按钮时,字符串传递给mainActivity并显示在textView On the mainActivity上。

现在我使用了一个线程,让用户等待5秒钟,显示消息等待5 ..然后替换5到4 ans等......然后当它出现0时显示答案[正确或错误] ..问题是当它达到4 ..不幸的是应用程序已经停止错误发生..

这是我的代码:

mainActivity.java

package com.example.radiobuttons;

import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;


public class MainActivity extends Activity  {

    TextView txt1;
    Button b1;
    int requestCode = 12;
    int j=1;
    String ANS;
    int t=5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Link();
        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this , secondActivity.class);
                startActivityForResult(in, requestCode);
            }
        });

    }

    public void Link()
    {
        txt1 = (TextView) findViewById(R.id.txtView1);
        b1 = (Button) findViewById(R.id.Button1);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if ( resultCode == 110)
        {
        ANS = data.getStringExtra("Message");
        String txt ="Your Answer Is = " + data.getStringExtra("Message");
        txt1.setText(txt);
        }
        th.start();
    }

    Thread th = new Thread()
    {
    public void run()
    {
        try
        {

                Thread.sleep(1000);
                t = t-1;


        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
        finally
        {
            txt1.setText("Wait" + t);
            if (t==0)
            {
                if(ANS.equals("Narender Modi"))
                {
                    txt1.setText("Your Answer Is Correct");
                }
                else
                {
                    txt1.setText("You Are Wrong.");
                }


            }
            else
            {
                th.start();
            }


        }
    }
    };



}

secondActivity.java

  package com.example.radiobuttons;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;

    public class secondActivity extends Activity {

        RadioGroup rg;
        Button b1;
        String ans;
        Intent in;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
            Link();

            rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub

                    switch (group.getId())
                    {
                    case R.id.radio0:
                        ans = "Manmohan Singh";

                        break;
                    case R.id.radio1:
                        ans = "Mulayam Singh";
                        break;
                    case R.id.radio2:
                        ans = "Narender Modi";
                        break;
                    }

                }
            });

            b1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    in = new Intent();
                    in.putExtra("answer", ans);
                    setResult(110,in);
                    finish();
                }
            });
        }

        public void Link()
        {
            rg = (RadioGroup) findViewById(R.id.radioGroup1);
            b1 = (Button) findViewById(R.id.Button1);

        }
    }

radioButtons Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.radiobuttons"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".secondActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.radiobutton.SECONDACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

logcat的:

06-20 15:12:24.846: E/AndroidRuntime(1952): FATAL EXCEPTION: Thread-117
06-20 15:12:24.846: E/AndroidRuntime(1952): Process: com.example.radiobuttons, PID: 1952
06-20 15:12:24.846: E/AndroidRuntime(1952): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6024)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:820)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.view.View.requestLayout(View.java:16431)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.view.View.requestLayout(View.java:16431)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.view.View.requestLayout(View.java:16431)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.view.View.requestLayout(View.java:16431)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.view.View.requestLayout(View.java:16431)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.widget.TextView.checkForRelayout(TextView.java:6600)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.widget.TextView.setText(TextView.java:3813)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.widget.TextView.setText(TextView.java:3671)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at android.widget.TextView.setText(TextView.java:3646)
06-20 15:12:24.846: E/AndroidRuntime(1952):     at com.example.radiobuttons.MainActivity$1.run(MainActivity.java:81)
06-20 15:12:26.882: I/Process(1952): Sending signal. PID: 1952 SIG: 9

activity_main.xml中

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.radiobuttons.MainActivity" >

    <TextView
        android:id="@+id/txtView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/Button1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        android:gravity="center_horizontal"
        android:layout_below="@+id/txtView1"
        android:text="Get Input From Second Activity" />

</RelativeLayout>

second.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="17dp"
        android:text="Prime Minister Of India?" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="21dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="Manmohan Singh" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mulayam Singh" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Narender Modi" />
    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginTop="39dp"
        android:text="Submit Answer" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

问题是,您正在与UI线程不同的线程中编辑视图。

所以有两种方法可以解决这个问题。

  1. 调用ExecuteInUiThread()的{​​{1}}方法更新TextView
  2. 使用Activity并更新AsyncTask方法
  3. 中的视图

答案 1 :(得分:0)

您应该在UI线程(主线程)中更新TextView。有几种方法可以完成此操作,例如,您可以使用活动的runOnUiThread()

runOnUiThread(new runnable(){
    @Override
    public void run(){
         // update that TextView here
    }
});