首先,我遵循了本教程:
我在这里检查了很多类似的帖子,但所有的解决方案都是特定于代码的,但没有用。
我刚刚开始开发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>
的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">test</string>
<string name="hello_world">"I got it to work!"</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
感谢您的帮助!如果您需要更多信息,请告诉我们。我仍然不确定解决这些错误所需要的是什么。
答案 0 :(得分:5)
在你的布局XML文件中,你需要确保textview
和edittext
是CamelCase,否则android将不知道如何给布局充气。这是因为android布局inflater尝试使用XML文件中指定的名称来实例化一个类。在这种情况下,android.view.TextView
和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>
答案 1 :(得分:3)
你需要注意拼写。这样的事情是敏感。
edittext
应该是
EditText
与其正下方的textview
元素相同。
应该是
TextView
答案 2 :(得分:0)
查看您的日志cat我会认为您收到此错误,因为edittext
应为EditText
,并且TextView