按钮不会工作[重复]

问题描述 投票:0回答:2

这个问题在这里已有答案:

我在第二个活动中添加了一个按钮,第三个,但是点击它时会出错。这是第一个活动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.
java android
2个回答
1
投票

你的第三个活动的java类是catagoryitalian但是在第二个活动的java类中的sendMessage方法中,你在Intent中传递了错误的类名

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

将其更改为

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

0
投票

在班级languageselection

public void sendMessage(View view) {
    Intent intent = new Intent(this, catagoryitalian.class);
   ...
}

您正在调用相同的Activity而不是第三个活动(如上所述)。

热门问题
推荐问题
最新问题