无法运行我的第一个Android应用程序

时间:2014-08-04 10:57:57

标签: java android eclipse

我从android网站下载了最新的adt包我已经有了Eclipse Juno和最新的ADT,我已经将最新的SDK和API导入了eclipse。

然后我打开并尝试在eclipse本身证明的内置app中运行示例,但从未得到输出。

然后我决定自己创建一个简单的应用程序,我创建了一个试图运行的小应用程序,即使这样我也无法获得任何输出。然后我通过你的网站,并得到一些建议说明使用“空布局”而不是“空白布局”。我也试过那个,但没有运气。我遇到另一个建议,说明API级别21存在问题,因此将其降级为19并尝试运行但没有运气。

下面是我使用的代码,添加了“try-catch”,只是为了看看我是否可以捕获一个正确的异常。

------- Mainactivity.java ---------

package com.suresh.firstapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    int c;
    Button add, sub; 
    TextView disp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c = 0;
        add = (Button) findViewById(R.id.bt_add);
        sub = (Button) findViewById(R.id.bt_sub);
        disp = (TextView) findViewById(R.id.textView1);
        try{
            add.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    c++;
                    disp.setText("Your total is: "+ c);
                }
            });
            sub.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    c--;
                    disp.setText("Your total is: "+ c);
                }
            });
        }
        catch(Exception e){

            System.out.println(e);

        }


    }
}

尝试使用此网站中的代码,但没有运气。

-------- 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="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="26dp"
        android:layout_marginTop="38dp"
        android:text="Add 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="22dp"
        android:text="Subtract 1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginTop="24dp"
        android:text="@string/stmt"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout> -->

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Your total is 0"
    android:textSize="40sp" />

    <Button
        android:id="@+id/bt_add"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="add one" />

    <Button
        android:id="@+id/bt_sub"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/bt_add"
        android:layout_below="@+id/bt_add"
        android:layout_marginTop="19dp"
        android:text="sub one" />
  </RelativeLayout>

当我尝试运行时,首先我的模拟器甚至没有开始大部分时间我收到一条消息说“未能打开HAX设备”或类似的东西,当它工作时我得到一条消息“不幸的是, FirstApp已停止“在logcat中出现以下错误

--------错误-----------

08-04 11:13:04.839: D/AndroidRuntime(1769): Shutting down VM
    08-04 11:13:04.839: W/dalvikvm(1769): threadid=1: thread exiting with uncaught exception (group=0xb0d71ce8)
    08-04 11:13:04.859: E/AndroidRuntime(1769): FATAL EXCEPTION: main
    08-04 11:13:04.859: E/AndroidRuntime(1769): Process: com.suresh.firstapp, PID: 1769
    08-04 11:13:04.859: E/AndroidRuntime(1769): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.suresh.firstapp/com.suresh.firstapp.MainActivity}: android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar.
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.app.ActivityThread.access$800(ActivityThread.java:138)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.os.Handler.dispatchMessage(Handler.java:102)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.os.Looper.loop(Looper.java:136)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.app.ActivityThread.main(ActivityThread.java:5026)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at java.lang.reflect.Method.invoke(Method.java:515)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at dalvik.system.NativeStart.main(Native Method)
    08-04 11:13:04.859: E/AndroidRuntime(1769): Caused by: android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar.
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:275)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2872)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3129)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:303)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.app.Activity.setContentView(Activity.java:1930)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at com.suresh.firstapp.MainActivity.onCreate(MainActivity.java:19)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.app.Activity.performCreate(Activity.java:5242)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
    08-04 11:13:04.859: E/AndroidRuntime(1769):     ... 11 more

--------------的AndroidManifest.xml ---------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.suresh.firstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

有人可以告诉我哪里出错了。

提前致谢

此致 苏雷什。

3 个答案:

答案 0 :(得分:1)

activity_main.xml 文件中的错误

您输入或粘贴了两个RelativeLayout根布局。 所有xml只有一个根元素。

答案 1 :(得分:0)

您在activity_main.xml

中使用了2个不同的根元素

但每个xml文档只应有一个ROOT ELEMENT

删除第二个RelativeLayout:

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

   </RelativeLayout>

并将所有其他xml标记添加到第一个<RelativeLayout />标记中,然后重试

答案 2 :(得分:0)

尝试将SDK tergetversion降级为

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />