按钮单击时更改xml活动

时间:2016-02-01 23:51:49

标签: java android xml

我在按钮点击时更改活动时遇到问题。我一直在关注YouTube视频,但我不知道为什么它不适合我。单击该按钮时,应用程序崩溃。这就是我所拥有的。

MainActivity.java

package com.example.andy.design;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

Button btnSimple = (Button) findViewById(R.id.btnSimple);
        btnSimple.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(), Activity2.class);
                startActivityForResult(intent, 0);
            }

        });


   //    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
      //  getMenuInflater().inflate(R.menu.menu_main, menu);
      //  return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Activity2.java

import android.app.Activity;
import android.os.Bundle;

public class Activity2 extends Activity {


    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);

    }


}

activity_main.xml中

<GridLayout
    android:layout_width="402dp"
    android:layout_height="match_parent"
    android:layout_row="5"
    android:layout_column="0"
    xmlns:android="http://schemas.android.com/apk/res/android">





    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/btnSimple"
        android:layout_row="15"
        android:layout_column="11" />


    </GridLayout>

logcat的

 02-01 23:16:19.500 2274-2274/com.example.andy.design I/art: Not late-enabling -Xcheck:jni (already on)
02-01 23:16:19.563 2274-2274/com.example.andy.design W/System: ClassLoader referenced unknown path: /data/app/com.example.andy.design-1/lib/x86
02-01 23:16:19.759 2274-2297/com.example.andy.design D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-01 23:16:19.824 2274-2297/com.example.andy.design I/OpenGLRenderer: Initialized EGL, version 1.4
02-01 23:16:20.163 2274-2297/com.example.andy.design W/EGL_emulation: eglSurfaceAttrib not implemented
02-01 23:16:20.163 2274-2297/com.example.andy.design W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabfa1a60, error=EGL_SUCCESS
02-01 23:16:20.462 2274-2274/com.example.andy.design I/Choreographer: Skipped 40 frames!  The application may be doing too much work on its main thread.
02-01 23:16:25.521 2274-2274/com.example.andy.design D/AndroidRuntime: Shutting down VM
02-01 23:16:25.522 2274-2274/com.example.andy.design E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.andy.design, PID: 2274
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.andy.design/com.example.andy.design.Activity2}: android.view.InflateException: Binary XML file line #13: Binary XML file line #13: Error inflating class activity
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                           at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                        Caused by: android.view.InflateException: Binary XML file line #13: Binary XML file line #13: Error inflating class activity
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                           at com.example.andy.design.Activity2.onCreate(Activity2.java:16)
                                                                           at android.app.Activity.performCreate(Activity.java:6237)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:148) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                        Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class activity
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                           at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                           at com.example.andy.design.Activity2.onCreate(Activity2.java:16) 
                                                                           at android.app.Activity.performCreate(Activity.java:6237) 
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:148) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                        Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.activity" on path: DexPathList[[zip file "/data/app/com.example.andy.design-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.andy.design-1/lib/x86, /vendor/lib, /system/lib]]
                                                                           at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                           at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                           at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                           at android.view.LayoutInflater.createView(LayoutInflater.java:583)
                                                                           at android.view.LayoutInflater.onCreateView(LayoutInflater.java:677)
                                                                           at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
                                                                           at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                           at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                           at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                           at com.example.andy.design.Activity2.onCreate(Activity2.java:16) 
                                                                           at android.app.Activity.performCreate(Activity.java:6237) 
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                           at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:148) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                        Suppressed: java.lang.ClassNotFoundException: android.view.activity
                                                                           at java.lang.Class.classForName(Native Method)
                                                                           at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                           at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                           at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                                ... 27 more
                                                                        Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

Android Manifest

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

    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <activity android:name=".Activity2"
        android:label="Activity Two"/>

4 个答案:

答案 0 :(得分:1)

您需要在AndroidManifest.xml中声明Activity2(在<application>开始和结束标记内部。)

这样的事情:

<activity android:name=".Activity2" 
          android:label="Activity Two"/>

Android文档中的此{​​{3}}概述了如何开始第二项活动。

答案 1 :(得分:0)

您的AndroidManifest.xml文件必须是这样的。

<?xml version="1.0" encoding="utf-8"?>

<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=".Activity2"
        android:label="Activity Two" />
</application>

答案 2 :(得分:0)

您可以尝试声明这样的上下文..

  

Context context = this;   然后使用此上下文来启动意图。   context.startActivity(yourIntent)

并尝试将此添加到您的清单。

<activity android:name=".Activity2">
    <intent-filter>
        <action android:name="android.intent.action.ACTIVITY2" />

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

答案 3 :(得分:0)

将Anmanifest中的Activity2.class条目作为活动。