获取Android FATAL EXCEPTION以下代码中的主要错误不知道为什么会发生这种情况。??有一个plz为我解释,plz plz帮助我自己发现错误 ...... !!
主要活动类: -
package www.gurjit.taskmanager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button add_button;
private View tasklist_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpElements(); // for setting up all the views/elements of the android page.
}
private void setUpElements() {
add_button = (Button)findViewById(R.id.addButton);
tasklist_view = findViewById(R.id.textView2);
add_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent in = new Intent(MainActivity.this, addActivityTask.class);
startActivity(in);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的Android_Main布局文件&不知道如何发生此错误 虽然按照相同的方式使用(Google API 4.3)Api级别: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/tasks"
android:textColor="#12ade4"
android:textSize="20sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#12ade4"
android:textSize="20sp"
android:layout_above="@+id/addButton"
android:layout_below="@id/textView1" />
<button
android:id="@id/addButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add_button"
android:textSize="15sp"
android:padding="10dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
下面是出现的logcat文件: -
01-28 09:12:45.150: D/AndroidRuntime(1766): Shutting down VM
01-28 09:12:45.150: W/dalvikvm(1766): threadid=1: thread exiting with uncaught exception (group=0x41465700)
01-28 09:12:45.180: E/AndroidRuntime(1766): FATAL EXCEPTION: main
01-28 09:12:45.180: E/AndroidRuntime(1766): java.lang.RuntimeException: Unable to start activity ComponentInfo{www.gurjit.taskmanager/www.gurjit.taskmanager.MainActivity}: android.view.InflateException: Binary XML file line #29: Error inflating class button
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.os.Looper.loop(Looper.java:137)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-28 09:12:45.180: E/AndroidRuntime(1766): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 09:12:45.180: E/AndroidRuntime(1766): at java.lang.reflect.Method.invoke(Method.java:525)
01-28 09:12:45.180: E/AndroidRuntime(1766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-28 09:12:45.180: E/AndroidRuntime(1766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-28 09:12:45.180: E/AndroidRuntime(1766): at dalvik.system.NativeStart.main(Native Method)
01-28 09:12:45.180: E/AndroidRuntime(1766): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class button
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-28 09:12:45.180: E/AndroidRuntime(1766): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.app.Activity.setContentView(Activity.java:1895)
01-28 09:12:45.180: E/AndroidRuntime(1766): at www.gurjit.taskmanager.MainActivity.onCreate(MainActivity.java:20)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.app.Activity.performCreate(Activity.java:5133)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-28 09:12:45.180: E/AndroidRuntime(1766): ... 11 more
01-28 09:12:45.180: E/AndroidRuntime(1766): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.button" on path: DexPathList[[zip file "/data/app/www.gurjit.taskmanager-2.apk"],nativeLibraryDirectories=[/data/app-lib/www.gurjit.taskmanager-2, /vendor/lib, /system/lib]]
01-28 09:12:45.180: E/AndroidRuntime(1766): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
01-28 09:12:45.180: E/AndroidRuntime(1766): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-28 09:12:45.180: E/AndroidRuntime(1766): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.view.LayoutInflater.createView(LayoutInflater.java:559)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
01-28 09:12:45.180: E/AndroidRuntime(1766): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
01-28 09:12:45.180: E/AndroidRuntime(1766): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
01-28 09:12:45.180: E/AndroidRuntime(1766): ... 21 more
答案 0 :(得分:2)
您的代码有多个问题:
在java代码中,您忘了转换TextView:
tasklist_view =(TextView)findViewById(R.id.textView2);
在XML中,将<button>
更改为<Button>
在ID属性中,您忘记在分配ID之前加上一个加号:
机器人:ID = “@ + ID / Add按钮”
在RelativeLayout
中,如果不声明相对定位,则无法使用视图,因此请将xml更改为:
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/tasks"
android:textColor="#12ade4"
android:textSize="20sp" />
<Button
android:id="@+id/addButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add_button"
android:textSize="15sp"
android:padding="10dp"
android:layout_alignParentBottom="true"/>