不幸的是(项目名称)已停止按下按钮

时间:2013-10-30 19:50:18

标签: android android-layout android-intent

  • 将呈现预定义问题的区域
  • 在屏幕上留下问题时会显示问题答案的按钮
  • 将提出答案的区域
  • 一个按钮,它会导致转换到格式与此格式相同的屏幕,并显示下一个问题
  • 将导致应用结束的按钮(转换为(3))

只需尝试从屏幕1转换到屏幕2,即可通过单击按钮在其位置使用不同的问题/答案对。如果除了切换屏幕和活动之外还有其它方法,请告诉我。

package com.example.androidassignment2;

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

public class AndroidAssignment2 extends Activity {

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

        Button next = (Button) findViewById(R.id.QButton);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, 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.android_assignment2_1, menu);
        return true;
    }

}

布局文件

<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:orientation="vertical" >

<TextView android:id="@+id/Questions"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:text="@string/Q2"   />

<Button android:id="@+id/QButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_question"  />

<Button android:id="@+id/AButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send" />

<TextView android:id="@+id/Answers"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:hint="@string/A2" />

<Button android:id="@+id/QuitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_quit" />


</LinearLayout>

活动1文件(需要)

package com.example.androidassignment2;

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

public class MainActivity extends Activity {

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


        Button next = (Button) findViewById(R.id.QButton);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
                startActivityForResult(myIntent, 0);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 }

2 个答案:

答案 0 :(得分:0)

Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
startActivityForResult(myIntent, 0);

您正在开始一项新活动,最可能的原因是该文件尚未在清单中声明。

在AndroidManifest.xml`中,您需要添加以下内容:

<activity
    android:name="your.package.name.AndroidAssignment2_1" />

这将驻留在<application>标记内。

答案 1 :(得分:0)

您正在使用startActivityForResult(),但未覆盖第一个onActivityForResult()中的Activity方法。您可以将其更改为仅使用startActivity()并从第二个setResult()中删除Activity

Button next = (Button) findViewById(R.id.QButton);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
            startActivity(myIntent);
        }
    });

如果这不是您的问题,那么请发布logcat(总是在遇到崩溃时),并确保Activity已经过期。

至于你的其他询问

  

如果除了切换错误的屏幕和活动之外还有其它方法,请告诉我。

如果您只是更改文字,则可以保持相同的Activity并更改TextView的文字。如果您愿意,可以将问题和答案存储在Array或数据库中,并保留一个计数器,并在每次点击时更改文本。

另一个选项,如果你愿意,可以与第一个选项一起使用,是使用ViewPager