【问题标题】:Unable to run my first android app无法运行我的第一个 Android 应用程序
【发布时间】:2014-08-04 10:57:57
【问题描述】:

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

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

然后我决定自己创建一个简单的应用程序,我创建了一个尝试运行的小应用程序,即使这样我也无法获得任何输出。然后我浏览了你的网站,得到了一些建议,说明使用“空布局”而不是“空白布局”。我也试过那个,但没有运气。我遇到了另一个建议,指出 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>

谁能告诉我哪里出错了。

提前致谢

问候, 苏雷什。

【问题讨论】:

标签: java android eclipse


【解决方案1】:

您的 activity_main.xml 文件中有错误

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

【讨论】:

  • 顶部的RelativeLayout被注释了,这是我实际使用过的,但我没有得到o/p。因此,评论了该部分并添加了我在此网站上找到的另一个 RelativeLayout 部分。
【解决方案2】:

您在 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 标签添加到第一个 &lt;RelativeLayout /&gt; 标签中,然后重试

【讨论】:

  • 顶部的RelativeLayout被注释了,这是我实际使用过的,但我没有得到o/p。所以,评论了那部分并添加了我在这个网站上找到的另一个 RelativeLayout 部分......
  • 如果你写 xmlns:android="schemas.android.com/apk/res/android" xmlns:tools="schemas.android.com/tools" 你的布局会自动成为根元素删除这个块。顺便说一句,你可以在你的根元素中使用任何布局你的 xml 文件。
【解决方案3】:

尝试将 SDK tergetversion 降级为

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    相关资源
    最近更新 更多