意图错误:单击按钮时

时间:2016-07-05 07:12:50

标签: android

我正在尝试使用Intent调用另一个活动。但是当我点击activityMain.xml的按钮时,它崩溃了。 你能弄明白这是什么问题吗?

MainActivity.java

package com.example.hacback17.framelayout;

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.ImageView;

public class MainActivity extends AppCompatActivity {

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

    private void doSomething(View v) {
        Intent intent = new Intent(MainActivity.this, NewActivity.class);
        startActivity(intent);

    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hacback17.framelayout.MainActivity">

    <Button

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Here"
        android:onClick="doSomething"/>
</RelativeLayout>

NewActivity.java

package com.example.hacback17.framelayout;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 * Created by Bablu on 7/5/2016.
 */
public class NewActivity extends AppCompatActivity {
    ImageView img1;
    ImageView img2;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_layout);
    }
}

new_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"/>

</FrameLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hacback17.framelayout">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".NewActivity"
            android:label="New Activity">
        </activity>

    </application>

</manifest>

logcat的

Process: com.example.hacback17.framelayout, PID: 30567
                                                                                   java.lang.IllegalStateException: Could not find method doSomething(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
                                                                                       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:321)
                                                                                       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:280)
                                                                                       at android.view.View.performClick(View.java:4444)
                                                                                       at android.view.View$PerformClick.run(View.java:18457)
                                                                                       at android.os.Handler.handleCallback(Handler.java:733)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                       at android.os.Looper.loop(Looper.java:136)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5113)
                                                                                       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:793)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                                       at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:0)

如果您想使用xml onclick,则该方法应为public

答案 1 :(得分:0)

private void doSomething(View v)替换为public void doSomething(View v)

答案 2 :(得分:0)

onClick事件应该声明为public。用以下代码替换doSomething()方法声明:

public void doSomething(View v) {
        Intent intent = new Intent(MainActivity.this, NewActivity.class);
        startActivity(intent);

    }

答案 3 :(得分:0)

或者你可以添加(id)到按钮,然后可以使用Button = new Button findVIewById(R.id.button);然后可以执行Button.setOnclickListner(new ON clickListner)