从菜单打开新意图时应用程序崩溃

时间:2014-06-10 04:52:47

标签: java android xml

所以我基本上都在关注本教程:https://www.youtube.com/watch?v=ZQxbOgHXM6M 这是我的文件,每当我进入菜单时,都会在我的手机上应用程序崩溃了。

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menuSweet:
        startActivity (new Intent("com.example.thebasics.SWEET"));
        return true;
    case R.id.menuToast:
        Toast andEggs = Toast.makeText(menu.this, "This is a toast", Toast.LENGTH_LONG);
        andEggs.show();
        return true;
    }
    return false;
}

这是^正在引用的甜蜜类

package com.example.thebasics;

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

public class Sweet extends Activity {

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

也是^引用的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <textView
        android:text="I like totally love you"
        android:id="@+id/tvAbot"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
    />
</LinearLayout>

清单

<activity
    android:name="com.example.thebasics.Sweet"
    android:label="@string/app_name" 
    android:theme="@android:style/Theme.Dialog">
    <intent-filter>
        <action android:name="com.example.thebasics.SWEET" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

logcat的

    06-10 16:56:43.573: D/AbsListView(8542): Get MotionRecognitionManager
06-10 16:56:43.713: D/AbsListView(8542): unregisterIRListener() is called 
06-10 16:56:43.718: D/AbsListView(8542): unregisterIRListener() is called 
06-10 16:56:43.753: D/AbsListView(8542): unregisterIRListener() is called 
06-10 16:56:46.793: D/AbsListView(8542): onDetachedFromWindow
06-10 16:56:46.813: E/ViewRootImpl(8542): sendUserActionEvent() mView == null
06-10 16:56:46.838: D/AndroidRuntime(8542): Shutting down VM
06-10 16:56:46.838: W/dalvikvm(8542): threadid=1: thread exiting with uncaught exception (group=0x41e1c700)
06-10 16:56:46.853: E/AndroidRuntime(8542): FATAL EXCEPTION: main
06-10 16:56:46.853: E/AndroidRuntime(8542): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thebasics/com.example.thebasics.Sweet}: android.view.InflateException: Binary XML file line #6: Error inflating class textView
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.app.ActivityThread.access$700(ActivityThread.java:159)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.os.Looper.loop(Looper.java:176)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.app.ActivityThread.main(ActivityThread.java:5419)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at java.lang.reflect.Method.invokeNative(Native Method)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at java.lang.reflect.Method.invoke(Method.java:525)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at dalvik.system.NativeStart.main(Native Method)
06-10 16:56:46.853: E/AndroidRuntime(8542): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class textView
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.app.Activity.setContentView(Activity.java:1956)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at com.example.thebasics.Sweet.onCreate(Sweet.java:12)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.app.Activity.performCreate(Activity.java:5372)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
06-10 16:56:46.853: E/AndroidRuntime(8542):     ... 11 more
06-10 16:56:46.853: E/AndroidRuntime(8542): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.textView" on path: /data/app/com.example.thebasics-1.apk
06-10 16:56:46.853: E/AndroidRuntime(8542):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.view.LayoutInflater.createView(LayoutInflater.java:565)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:658)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
06-10 16:56:46.853: E/AndroidRuntime(8542):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
06-10 16:56:46.853: E/AndroidRuntime(8542):     ... 21 more
06-10 16:56:57.413: I/Process(8542): Sending signal. PID: 8542 SIG: 9

4 个答案:

答案 0 :(得分:0)

你做错了。试试这个:

startActivity (new Intent(this,Sweet.class));

答案 1 :(得分:0)

尝试更改

<textView
        android:text="I like totally love you"
        android:id="@+id/tvAbot"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
    /> 

<TextView
        android:text="I like totally love you"
        android:id="@+id/tvAbot"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
    />

是否有名称为 SWEET 的课程?我认为你应该改变startActivity (new Intent("com.example.thebasics.SWEET"));

startActivity (new Intent(this,Sweet.class));。因为logcat显示ClassNotFoundException

答案 2 :(得分:0)

如果您在xml文件中使用它

 <textView
    android:text="I like totally love you"
    android:id="@+id/tvAbot"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
/>
然后它完全错了。一定是

  <TextView
    android:text="I like totally love you"
    android:id="@+id/tvAbot"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
/>

也可以从

更改此内容
   startActivity (new Intent("com.example.thebasics.SWEET"));

   startActivity (new Intent(this,Sweet.class));

答案 3 :(得分:0)

只需将textView更改为TextView

即可

更改此行

startActivity (new Intent("com.example.thebasics.SWEET"));

到此:

startActivity (new Intent(this,Sweet.class));