按钮不会工作

我已经添加了一个按钮,从我的第二个活动和第三,但它会提供一个错误,当它被点击。 这是第一个活动的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.

回答:

在类languageselection

public void sendMessage(View view) { 

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

...

}

要调用同一个活动,而不是第三个活动(如上) 。

回答:

你的第三个活动的java类是catagoryitalian但在第二个活动的java类里面sendMessage方法,你在Intent

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

更改它传递了错误的类名

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

以上是 按钮不会工作 的全部内容, 来源链接: utcz.com/qa/266899.html

回到顶部