我試圖讓啓動在MainActivity當用戶點擊該按鈕secondActivity打開並顯示三個單選按鈕單選按鈕組一個簡單的Android程序。當用戶點擊單選按鈕並按下按鈕時,該字符串傳遞給mainActivity,並應顯示在textView On mainActivity上。不幸的是,應用程序的名字已停止線程錯誤
現在我已經使用使用戶通過顯示消息,等待5秒鐘一個線程等待5 ..然後更換5至4答等等......,最後,當談到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);
}
}
單選按鈕清單
<?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>
你爲什麼要標明這個問題進行復制? – Akshay
它被標記爲重複,因爲它是重複的。 – hexafraction
你能告訴我鏈接的地方有同樣的問題嗎? – Akshay