当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Unfortunately, "app name here" has stopped - followed tutorial

问题描述:

To begin with, I followed this tutorial:

http://www.codeproject.com/Articles/102065/Android-A-beginner-s-guide?fid=1583614&df=90&mpp=10&noise=1&prof=True&sort=Position&view=Expanded&spc=None&fr=11#xx0xx

I have checked a good many of the similar posts here, but all of the solutions have been code specific and haven't worked.

I am just starting out developing android apps, and found this tutorial. When I launch it though, the emulator gives me the this app has stopped message. There are no warnings from my code, so I don't even know where to begin.

Here's my code:

MainActivity.java

package com.example.test;

import android.app.Activity;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.Menu;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_main, menu);

//return true;

//get the two controls we created earlier, also with the resource reference and the id

final TextView lv_View = (TextView)findViewById(R.id.lv_View);

lv_View.setText("4");

final EditText et_Text = (EditText)findViewById(R.id.et_Text);

//add new KeyListener Callback (to record key input)

et_Text.setOnKeyListener(new View.OnKeyListener()

{

//function to invoke when a key is pressed

public boolean onKey(View v, int keyCode, KeyEvent event)

{

//check if there is

if (event.getAction() == KeyEvent.ACTION_DOWN)

{

//check if the right key was pressed

if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)

{

//add the text to the text view

lv_View.setText(lv_View.getText() + ", " +

et_Text.getText());

//and clear the EditText control

et_Text.setText("");

return true;

}

}

return false;

}

});

return true;

}

}

test manifest

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.test"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="15" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.example.test.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

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

</intent-filter>

</activity>

</application>

</manifest>

strings.xml

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

<resources>

<string name="app_name">test</string>

<string name="hello_world">&quot;I got it to work!&quot;</string>

<string name="menu_settings">Settings</string>

</resources>

activity_main.xml (layout)

<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"

tools:context=".MainActivity"

>

<edittext android:layout_height="wrap_content"

android:layout_width="fill_parent" android:textColor="#FF0000FF"

android:id="@+id/et_Text" />

<textview android:layout_height="wrap_content" android:layout_width="fill_parent"

android:textColor="#FF00FF00" android:id="@+id/lv_View" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:textColor="#FF00FF00"

android:text="@string/hello_world"

/>

</RelativeLayout>

LogCat report

12-18 21:34:37.873: W/Trace(713): Unexpected value from nativeGetEnabledTags: 0

12-18 21:34:38.442: W/Trace(713): Unexpected value from nativeGetEnabledTags: 0

12-18 21:34:38.522: W/Trace(713): Unexpected value from nativeGetEnabledTags: 0

12-18 21:34:39.562: D/AndroidRuntime(713): Shutting down VM

12-18 21:34:39.602: W/dalvikvm(713): threadid=1: thread exiting with uncaught exception (group=0x40a70930)

12-18 21:34:39.812: E/AndroidRuntime(713): FATAL EXCEPTION: main

12-18 21:34:39.812: E/AndroidRuntime(713): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class edittext

12-18 21:34:39.812: E/AndroidRuntime(713): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.app.ActivityThread.access$600(ActivityThread.java:141)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.os.Handler.dispatchMessage(Handler.java:99)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.os.Looper.loop(Looper.java:137)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.app.ActivityThread.main(ActivityThread.java:5039)

12-18 21:34:39.812: E/AndroidRuntime(713): at java.lang.reflect.Method.invokeNative(Native Method)

12-18 21:34:39.812: E/AndroidRuntime(713): at java.lang.reflect.Method.invoke(Method.java:511)

12-18 21:34:39.812: E/AndroidRuntime(713): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

12-18 21:34:39.812: E/AndroidRuntime(713): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

12-18 21:34:39.812: E/AndroidRuntime(713): at dalvik.system.NativeStart.main(Native Method)

12-18 21:34:39.812: E/AndroidRuntime(713): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class edittext

12-18 21:34:39.812: E/AndroidRuntime(713): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)

12-18 21:34:39.812: E/AndroidRuntime(713): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.app.Activity.setContentView(Activity.java:1881)

12-18 21:34:39.812: E/AndroidRuntime(713): at com.example.test.MainActivity.onCreate(MainActivity.java:17)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.app.Activity.performCreate(Activity.java:5104)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

12-18 21:34:39.812: E/AndroidRuntime(713): ... 11 more

12-18 21:34:39.812: E/AndroidRuntime(713): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.edittext" on path: /data/app/com.example.test-2.apk

12-18 21:34:39.812: E/AndroidRuntime(713): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)

12-18 21:34:39.812: E/AndroidRuntime(713): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)

12-18 21:34:39.812: E/AndroidRuntime(713): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.view.LayoutInflater.createView(LayoutInflater.java:552)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)

12-18 21:34:39.812: E/AndroidRuntime(713): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)

12-18 21:34:39.812: E/AndroidRuntime(713): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)

12-18 21:34:39.812: E/AndroidRuntime(713): ... 21 more

Thanks for any help! If you need any more information, just let me know. I'm still not sure of what's needed to trouble shoot these kinds of errors.

网友答案:

In your layout XML file, you need to ensure that textview and edittext are CamelCase otherwise android will not know how to inflate the layout. This is because the android layout inflater attempts to instance a class by using the name specifed in the XML file. In this case, android.view.TextView and android.view.EditText.

<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"
    tools:context=".MainActivity">

    <EditText android:layout_height="wrap_content" 
         android:layout_width="fill_parent"
         android:textColor="#FF0000FF"
         android:id="@+id/et_Text" />

    <TextView android:layout_height="wrap_content"
         android:layout_width="fill_parent"
         android:textColor="#FF00FF00"
         android:id="@+id/lv_View" />

    <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:textColor="#FF00FF00"
         android:text="@string/hello_world" />

</RelativeLayout>
网友答案:

You need to watch your spelling. Things like this are case sensitive.

edittext

should be

EditText

Same with the textview element directly below it.

It should be

TextView
网友答案:

Looking at your log cat I would think that you are getting this error because edittext should be EditText, and the sane with TextView

分享给朋友:
您可能感兴趣的文章:
随机阅读: