gpt4 book ai didi

java - 按钮不起作用

转载 作者:行者123 更新时间:2023-12-02 11:49:10 25 4
gpt4 key购买 nike

我从第二个 Activity 和第三个 Activity 中添加了一个按钮,但单击它时出现错误。这是第一个 Activity Java 文件。

package com.example.ephraimcohen.prestwichlanguageschool;

import android.content.Intent;
import android.provider.AlarmClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import static android.provider.AlarmClock.EXTRA_MESSAGE;

public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = AlarmClock.EXTRA_MESSAGE ;

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

/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, languageselection.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}

这是第二个 Java 文件

package com.example.ephraimcohen.prestwichlanguageschool;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static android.provider.AlarmClock.EXTRA_MESSAGE;
import static android.support.v4.app.NotificationCompat.EXTRA_MESSAGES;

public class languageselection extends AppCompatActivity {


public static final String EXTRA_MESSAGE ="extra_message";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_languageselection);

// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}

/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, languageselection.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}

这是第三个 Java 文件。

package com.example.ephraimcohen.prestwichlanguageschool;

import android.app.Notification;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.Locale;

public class catagoryitalian extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_catagoryitalian);

// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(languageselection.EXTRA_MESSAGE);

// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}

我已设法通过按钮将第一页连接到第二页,但第二页不允许我转到第三页。我已将所有代码放入其中,但当按下第二页上的按钮时,它只会重新启动应用程序。重新启动后,它会关闭该应用程序。任何建议将不胜感激。

第二个 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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_width="match_parent"
android:layout_height="match_parent"


tools:context="com.example.ephraimcohen.prestwichlanguageschool.languageselection">

<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="77dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo"
android:contentDescription="@string/todo" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="265dp"
android:layout_marginStart="81dp"
android:layout_marginTop="2dp"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/italianflag" android:contentDescription="@string/todo"
android:layout_marginRight="265dp"
android:layout_marginLeft="81dp" />

<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.857"
app:srcCompat="@drawable/polishflag" android:contentDescription="@string/todo" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:layout_marginTop="25dp"
android:text="@string/italian"
android:onClick="sendMessage"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:layout_marginLeft="34dp" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="22dp"
android:layout_marginStart="34dp"
android:text="@string/polish"
app:layout_constraintBottom_toBottomOf="@+id/imageView4"
app:layout_constraintStart_toEndOf="@+id/imageView4"
android:layout_marginLeft="34dp" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="11dp"
android:layout_marginStart="106dp"
android:text="@string/choose_a_language"
android:textAppearance="@style/TextAppearance.AppCompat.Display2"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="11dp"
android:layout_marginLeft="106dp" />
</android.support.constraint.ConstraintLayout>

堆栈跟踪

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ephraimcohen.prestwichlanguageschool, PID: 30038
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5640)
at android.view.View$PerformClick.run(View.java:22455)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5640) 
at android.view.View$PerformClick.run(View.java:22455) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6165) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.ephraimcohen.prestwichlanguageschool.languageselection.sendMessage(languageselection.java:39)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5640) 
at android.view.View$PerformClick.run(View.java:22455) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6165) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 
Application terminated.

最佳答案

您的第三个 Activity 的java类是catagoryitalian,但在第二个 Activity 的java类内的sendMessage方法中,您在Intent中传递了错误的类名

Intent intent = new Intent(this, languageselection.class);

更改为

Intent intent = new Intent(this, catagoryitalian.class);

关于java - 按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47997024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com