0

首先,我按照本教程进行操作:

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

我在这里检查了很多类似的帖子,但是所有的解决方案都是特定于代码的并且没有奏效。

我刚刚开始开发 android 应用程序,并找到了本教程。但是,当我启动它时,模拟器会给我这个应用程序已停止的消息。我的代码没有警告,所以我什至不知道从哪里开始。

这是我的代码: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;
} 
}

测试清单

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

字符串.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(布局)

<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 报告

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

谢谢你的帮助!如果您需要更多信息,请告诉我。我仍然不确定解决这些错误需要什么。

4

3 回答 3

5

在您的布局 XML 文件中,您需要确保textviewedittext是 CamelCase,否则 android 将不知道如何扩展布局。这是因为 android layout inflater 尝试使用 XML 文件中指定的名称来实例化一个类。在这种情况下,android.view.TextViewandroid.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>
于 2012-12-18T21:58:05.627 回答
3

你需要注意你的拼写。像这样的事情是区分大小写的。

edittext

应该

EditText

textview它正下方的元素相同。

它应该是

TextView
于 2012-12-18T21:58:03.210 回答
0

查看您的日志猫,我认为您收到此错误是因为edittext应该是EditText,并且理智TextView

于 2012-12-18T22:00:16.687 回答