使用按钮单击将一个活动链接到另一个活动

Posted

技术标签:

【中文标题】使用按钮单击将一个活动链接到另一个活动【英文标题】:Link one activity to another using a button click 【发布时间】:2017-09-30 01:08:56 【问题描述】:

我有一个家庭活动、QuizActivity、ResultActivity 及其 xml 文件。

我在主页活动中放置了一个按钮以链接到 QuestionActivity 以开始测验。

点击按钮后应用崩溃

“不幸的是,……已经停止了”

下面是代码。

activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="@mipmap/kiss_home"
tools:context=".Home">

<TextView
    android:id="@+id/textView"
    android:layout_
    android:layout_
    android:text="Are you a good Kisser?"
    android:textSize="22dp"
    android:textColor="@android:color/holo_blue_bright"
    tools:layout_editor_absoluteX="76dp"
    tools:layout_editor_absoluteY="22dp"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/button"
    android:layout_alignEnd="@+id/button"
    android:layout_marginTop="53dp" />

<Button
    android:id="@+id/button"
    android:layout_
    android:layout_
    android:text="@string/start_quiz"
    android:textSize="20dp"
    android:background="@color/colorAccent"
    tools:layout_editor_absoluteX="74dp"
    tools:layout_editor_absoluteY="207dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="50dp"
    android:layout_marginStart="50dp"
    android:layout_marginBottom="128dp" 
    android:onClick="onClick"/>

    </RelativeLayout>

Home.java

包 com.webkenni.goodkisser.goodkisser;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Home extends Activity 
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    button=(Button)findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener()
    
        public void onClick(View v)
        
            Intent myIntent = new Intent(Home.this,QuizActivity.class);
            startActivity(myIntent);
        
    );



    QuizActivity

 package com.webkenni.goodkisser.goodkisser;

 /**
  * Created by user on 5/2/2017.
   */


 import java.util.List;
 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Intent;
 import android.util.Log;
 import android.view.Menu;
 import android.view.View;
  import android.widget.Button;
  import android.widget.RadioButton;
  import android.widget.RadioGroup;
  import android.widget.TextView;
  public class QuizActivity extends Activity 
    List<Question> quesList;
    int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    DbHelper db=new DbHelper(this);
    quesList=db.getAllQuestions();
    currentQ=quesList.get(qid);
    txtQuestion=(TextView)findViewById(R.id.textView1);
    rda=(RadioButton)findViewById(R.id.radio0);
    rdb=(RadioButton)findViewById(R.id.radio1);
    rdc=(RadioButton)findViewById(R.id.radio2);
    butNext=(Button)findViewById(R.id.button1);
    setQuestionView();
    butNext.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
            Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
            if(currentQ.getANSWER().equals(answer.getText()))
            
                score++;
                Log.d("score", "Your score"+score);
            
            if(qid<5)
                currentQ=quesList.get(qid);
                setQuestionView();
            else
                Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
                Bundle b = new Bundle();
                b.putInt("score", score); //Your score
                intent.putExtras(b); //Put your score to your next Intent
                startActivity(intent);
                finish();
            
        
    );
    
   @Override
   public boolean onCreateOptionsMenu(Menu menu) 
   // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_quiz, menu);
    return true;
    
    private void setQuestionView()
    
    txtQuestion.setText(currentQ.getQUESTION());
    rda.setText(currentQ.getOPTA());
    rdb.setText(currentQ.getOPTB());
    rdc.setText(currentQ.getOPTC());
    rdc.setText(currentQ.getOPTD());
    qid++;
    
     

   activity_quiz.xml

   <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_
      android:layout_
      tools:context=".QuizActivity" >
      <LinearLayout
       android:layout_
       android:layout_
       android:layout_alignParentBottom="true"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true"
       android:orientation="vertical" >
       <TextView
        android:id="@+id/textView1"
        android:layout_
        android:layout_
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
        <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_
        android:layout_
        android:layout_weight="0.04" >
          <RadioButton
            android:id="@+id/radio0"
            android:layout_
            android:layout_
            android:checked="true"
            android:text="RadioButton" />
          <RadioButton
            android:id="@+id/radio1"
            android:layout_
            android:layout_
            android:text="RadioButton" />
        <RadioButton
            android:id="@+id/radio2"
            android:layout_
            android:layout_
            android:text="RadioButton" />
        </RadioGroup>
        <Button
        android:id="@+id/button1"
        android:layout_
        android:layout_
        android:text="@string/str_next" />
   </LinearLayout>
  </RelativeLayout>


  Manifest file

   <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.webkenni.goodkisser.goodkisser">

      <application
     android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Home">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
     </activity>
    <activity android:name=".QuizActivity" >
    </activity>
    <activity android:name=".ResultActivity" >
    </activity>
    </application>

   </manifest>

【问题讨论】:

你的代码在哪里?? 为什么android:onClick="onClick" 按钮标签之外? onClick()方法和QuestionActivity的post code 请找到更新后的代码,应用也有同样的错误 【参考方案1】:
<Button
    android:id="@+id/button"
    android:layout_
    android:layout_
    android:text="@string/start_quiz"
    android:textSize="20dp"
    android:background="@color/colorAccent"

    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="50dp"
    android:layout_marginStart="50dp"
    android:layout_marginBottom="128dp" 
   />


 btn_submit.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View view) 
                    Intent myIntent = new Intent(Home.this,QuizActivity.class);
        startActivity(myIntent);
                
            );

【讨论】:

以上是关于使用按钮单击将一个活动链接到另一个活动的主要内容,如果未能解决你的问题,请参考以下文章

无法从一个活动获取数据到另一个活动

如何将 LatLng 的数组列表从一个活动传递到另一个活动

如何将来自 TextViews 中动态创建的 EditTexts 的用户输入显示到另一个活动中?

如何从一个屏幕导航到另一个屏幕

ActivityNotFoundException,启动活动时出错

钛合金背活性