- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我在第二个 Activity 中点击“返回”按钮时,我的应用程序崩溃了。基本上,当我在第二个 Activity 中点击“返回”按钮时,它会显示“应用程序意外崩溃”,我需要点击“确定”,但无论如何它都会返回到第一个 Activity 。我希望它能够毫无错误地进入第一个 Activity ,或者根本离开应用程序。
这是我的 MainActivity.java:
public class MainActivity extends AppCompatActivity {
TextView TV_English,TV_Polish;
Locale mylocale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TV_English=(TextView)findViewById(R.id.TVEnglish);
TV_Polish=(TextView)findViewById(R.id.TVPolish);
//Set English Language
TV_English.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"English Language",Toast.LENGTH_SHORT).show();
setLanguage("en");
}
});
//Set Polish Language
TV_Polish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Język polski",Toast.LENGTH_SHORT).show();
setLanguage("pl");
}
});
}
//Change language Method
protected void setLanguage(String language){
mylocale=new Locale(language);
Resources resources=getResources();
DisplayMetrics dm=resources.getDisplayMetrics();
Configuration conf= resources.getConfiguration();
conf.locale=mylocale;
resources.updateConfiguration(conf,dm);
Intent refreshIntent=new Intent(MainActivity.this,MainActivity.class);
finish();
startActivity(refreshIntent);
}
//This method opens QuizActivity
public void startQuiz(View view) {
Intent mainIntent = new Intent(this, pl.nataliana.knowyourwhale.QuizActivity.class);
startActivity(mainIntent);
EditText txtname = (EditText) findViewById(R.id.textName);
String name = txtname.getText().toString();
Intent i = new Intent(this, pl.nataliana.knowyourwhale.QuizActivity.class);
i.putExtra("name", name);
startActivity(i);
}
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, R.string.exit_press_back_twice_message, Toast.LENGTH_SHORT).show();
}
}
这是我的第二个 Activity (QuizActivity.java):
//This is an app for Whale Quiz - after answering all questions you will know how much do you know about whales.
public class QuizActivity extends AppCompatActivity {
// variables
private int score = 0;
public String introMessage;
public TextView intro;
public RadioButton trueQ1;
public RadioButton falseQ1;
public RadioButton trueQ2;
public RadioButton falseQ2;
public RadioButton poland;
public RadioButton island;
public CheckBox A3a;
public CheckBox A3b;
public CheckBox A3c;
public CheckBox A3d;
public EditText textAnswer;
//this method is called when Start Quiz button is clicked
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
Intent intent = getIntent();
String name = intent.getExtras().getString("name");
intro = (TextView) findViewById(R.id.intro);
trueQ1 = (RadioButton) findViewById(R.id.trueQ1);
falseQ1 = (RadioButton) findViewById(R.id.falseQ1);
trueQ2 = (RadioButton) findViewById(R.id.trueQ2);
falseQ2 = (RadioButton) findViewById(R.id.falseQ2);
poland = (RadioButton) findViewById(R.id.Poland);
island = (RadioButton) findViewById(R.id.Island);
A3a = (CheckBox) findViewById(R.id.A3a);
A3b = (CheckBox) findViewById(R.id.A3b);
A3c = (CheckBox) findViewById(R.id.A3c);
A3d = (CheckBox) findViewById(R.id.A3d);
introMessage = getString(R.string.Hello) + " " + name + getString(R.string.initial);
intro.setText(introMessage);
}
//this method is called when the user answers 1st question
public void clickQ1(View view) {
if (trueQ1.isChecked()) {
score++;
}
}
//this method is called when the user answers 2nd question
public void clickQ2(View view) {
if (trueQ2.isChecked()) {
score++;
}
}
//those methods are called when the user answers 3rd question
public void onCheckboxClickedA(View view) {
if (A3a.isChecked()) {
score++;
}
}
public void onCheckboxClickedB(View view) {
if (A3b.isChecked()) {
score++;
}
}
public void onCheckboxClickedC(View view) {
if (A3c.isChecked()) {
//wrong answer - do nothing
}
}
public void onCheckboxClickedD(View view) {
if (A3d.isChecked()) {
score++;
}
}
//this method is called to check 4th question
public void freeWilly() {
EditText textAnswer = (EditText) findViewById(R.id.textAnswer);
String willy = textAnswer.getText().toString();
Boolean booleanWilly = willy.equalsIgnoreCase(getString(R.string.Willy));
if (booleanWilly) {
score++;
}
}
//this method is called when the user answers 5th question
public void clickQ5(View view) {
if (island.isChecked()) {
score++;
}
}
//this method is called when the user clicks submit button
public void submit(View view) {
freeWilly();
Toast.makeText(this, getString(R.string.score) + " " + score + getString(R.string.for7), Toast.LENGTH_LONG).show();
score = 0;
}
//this method is called when the user clicks answers button
public void answers(View view) {
trueQ1 = (RadioButton) findViewById(R.id.trueQ1);
falseQ1 = (RadioButton) findViewById(R.id.falseQ1);
trueQ2 = (RadioButton) findViewById(R.id.trueQ2);
falseQ2 = (RadioButton) findViewById(R.id.falseQ2);
A3a = (CheckBox) findViewById(R.id.A3a);
A3b = (CheckBox) findViewById(R.id.A3b);
A3c = (CheckBox) findViewById(R.id.A3c);
A3d = (CheckBox) findViewById(R.id.A3d);
poland = (RadioButton) findViewById(R.id.Poland);
island = (RadioButton) findViewById(R.id.Island);
textAnswer = (EditText) findViewById(R.id.textAnswer);
trueQ1.setTextColor(GREEN);
falseQ1.setTextColor(Color.RED);
trueQ2.setTextColor(GREEN);
falseQ2.setTextColor(Color.RED);
A3a.setTextColor(GREEN);
A3b.setTextColor(GREEN);
A3c.setTextColor(Color.RED);
A3d.setTextColor(GREEN);
poland.setTextColor(Color.RED);
island.setTextColor(GREEN);
textAnswer.setText(R.string.Willy);
textAnswer.setTextColor(GREEN);
}
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, R.string.exit_press_back_twice_message, Toast.LENGTH_SHORT).show();
}
}
@编辑
以下崩溃报告:
04-10 14:58:35.398 6442-6442/pl.nataliana.knowyourwhale D/AndroidRuntime: Shutting down VM
04-10 14:58:35.398 6442-6442/pl.nataliana.knowyourwhale W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4162ee00)
04-10 14:58:35.418 6442-6442/pl.nataliana.knowyourwhale E/AndroidRuntime: FATAL EXCEPTION: main
Process: pl.nataliana.knowyourwhale, PID: 6442
java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.nataliana.knowyourwhale/pl.nataliana.knowyourwhale.QuizActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at pl.nataliana.knowyourwhale.QuizActivity.onCreate(QuizActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5340)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711)
at dalvik.system.NativeStart.main(Native Method)
最佳答案
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent=new Intent(QuizActivity.this,MainActivity .class);
startActivity(intent);
finish();
}
尝试这种方式。因为您没有将 Intent 的 Activity 名称传递给下一个 Activity 。
关于java - 当我在一项 Activity 中点击“返回”按钮时,我的应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43323636/
我对 Android 很陌生,如果问题重复,请避免并发送链接。有三个 Activity A、B 和 C。 Activity A 获取一个用户名,我想在 Activity C 中显示该用户名,但我想先运
我正在尝试制作记事本应用程序,因此每次打开新笔记时,布局都会相同。另外, Activity 的数量(新注释)不应定义得尽可能多 最佳答案 如果 Activity 始终相同,您可能应该创建一个适配器,允
我有 3 个 Activity 。 主窗口 5 个按钮 在按钮的主窗口中按下此窗口打开(将其称为父窗口) 在父窗口按钮上按下此窗口打开调用它作为结束子窗口。 现在从子窗口我从父窗口获取值如下:
我遇到了一个 Activity backstack 问题。假设我的后台有 5 个 Activity :比如 Activity A、 Activity B、 Activity C、 Activity D
我正在寻找必须具有以下附加特征的 JMS 提供程序: 采用多代理,所有代理都必须处于事件状态(无单点故障) 仅在两台机器上进行扩展就足以满足我们的需求 能够保证订购(如果 1 个生产者 + 1 个消费
假设,我有一个由 TabHost 组成的选项卡 Activity 。 TabHost 包含 2 个选项卡,每两个选项卡都有一个 Activity 组。每个 Activity 组包含一项 Activit
我正在开发一个应用程序,我需要根据某些操作导航到特定 Activity 。这是一张图片 我的第一个 Activity 是 ReadingActivity。基于某些操作,用户将被带到 NewProjec
我创建了一个与服务器异步通信的应用程序。当应用程序发出服务器请求时,将创建一个带有“正在加载”通知的新对话框( Activity )。主要 Activity 实现了处理服务器响应的方法,我想在主要 A
我想在我的所有应用程序 Activity 中显示相同的选项菜单。我创建了一个实现菜单的通用 Activity ,并且我所有的进一步 Activity 都扩展了它。 问题:当我需要扩展其他特定 Acti
我有四个 Activity ,即 java 文件 - Activity1.java、activity2.java、activity3.java、activity4.java 和 xml 文件 - Ac
我有两个 Activity 。我想将数据从第二个 Activity 发送到上一个 Activity 。第一个 Activity 有自定义 ListView 和 bean 类。当我点击第二个 Activ
根 Activity 是堆栈中当前的第一个 Activity 还是 list 中指定为启动 Activity 的 Activity ? 支持应用程序 P 在启动时启动 Activity A。然后 A
你好 我想知道您在绘制 Activity 图选择“Activity ”时考虑了哪些关键点? 您如何从要建模的问题中选择 Activity ? 谢谢 最佳答案 Activity 图用于对正在开发的系统和
如何从主 Activity 启动 Activity 并在子 Activity 返回主 Activity 中退出操作后返回主 Activity ? 我已将子 Activity 作为启动器 Intent
我的工作流程如下: 登录 Activity -> ActivityB -> ActivityC -> ActivityD 我想将数据从LoginActivity传递到ActivityD,但不直接传递到
我之前曾尝试获得此问题的答案,但找不到可以解决我的问题的答案。我正在制作保存圆盘高尔夫球分数的应用程序。我的 MainActivity 有 4 个按钮。新比赛、恢复比赛、类(class)和球员。 At
我有一个 tts 非 UI 类和 Activity 类。现在在 Activity 类中,我有一个按钮,用户可以从中选择男声或女声,具体取决于我想要将字符串传递给 tts 类的选择,然后一次tts 类根
问题有点复杂,首先, Activity A 和 Activity B 的 list 中都有 android:noHistory = true 。我有一个自定义 serialized 类,假设 MyCl
在我的应用程序中,我有两个 Activity (AuthenticationActivity 和 MainActivity),每个 Activity 都有一个导航图和大量 fragment 。我创建了
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How can i use compose email activity in tabView? 我想在选项
我是一名优秀的程序员,十分优秀!