安卓应用程序"Hello World"问题



不知道这个应用程序出了什么问题。看遍了整个互联网,在过去的3天里一直在试图解决这个问题,一直在看整个互联网。控制台的最后两行如下所示:

[2014-07-29 01:46:01 - HelloWorld] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.helloworld/.MainActivity }
[2014-07-29 01:46:01 - HelloWorld] ActivityManager: Warning: Activity not started, its current task has been brought to the front

Java:

package com.example.helloworld;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void button_touch(View v)
    {
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setText("Hello, world.");
    }
}
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"
    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="com.example.helloworld.MainActivity" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="143dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="67dp"
        android:onClick="button_touch"
        android:text="Say hi" />

</RelativeLayout>

这里是更多的控制台:

{[2014-07-29 01:45:50 - HelloWorld] ------------------------------
[2014-07-29 01:45:50 - HelloWorld] Android Launch!
[2014-07-29 01:45:50 - HelloWorld] adb is running normally.
[2014-07-29 01:45:50 - HelloWorld] Performing com.example.helloworld.MainActivity activity launch
[2014-07-29 01:45:50 - HelloWorld] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Level20'
[2014-07-29 01:45:52 - HelloWorld] ------------------------------
[2014-07-29 01:45:52 - HelloWorld] Android Launch!
[2014-07-29 01:45:52 - HelloWorld] adb is running normally.
[2014-07-29 01:45:52 - HelloWorld] Performing com.example.helloworld.MainActivity activity launch
[2014-07-29 01:45:53 - HelloWorld] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Level20'
[2014-07-29 01:45:54 - HelloWorld] Application already deployed. No need to reinstall.
[2014-07-29 01:45:54 - HelloWorld] Starting activity com.example.helloworld.MainActivity on device emulator-5554
[2014-07-29 01:45:56 - HelloWorld] ActivityManager: WARNING: linker: libdvm.so has text relocations. This is wasting memory and prevents security hardening. Please fix.
[2014-07-29 01:45:59 - HelloWorld] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.helloworld/.MainActivity }
[2014-07-29 01:45:59 - HelloWorld] Application already deployed. No need to reinstall.
[2014-07-29 01:45:59 - HelloWorld] Starting activity com.example.helloworld.MainActivity on device emulator-5554
[2014-07-29 01:45:59 - HelloWorld] ActivityManager: WARNING: linker: libdvm.so has text relocations. This is wasting memory and prevents security hardening. Please fix.
[2014-07-29 01:46:01 - HelloWorld] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.helloworld/.MainActivity }
[2014-07-29 01:46:01 - HelloWorld] ActivityManager: Warning: Activity not started, its current task has been brought to the front
}

这是logcat:

07-29 06:49:01.152: I/Process(1711): Sending signal. PID: 1711 SIG: 9
07-29 06:49:02.522: D/AndroidRuntime(1770): Shutting down VM
07-29 06:49:02.522: W/dalvikvm(1770): threadid=1: thread exiting with uncaught exception (group=0xb0d3bce8)
07-29 06:49:02.532: E/AndroidRuntime(1770): FATAL EXCEPTION: main
07-29 06:49:02.532: E/AndroidRuntime(1770): Process: com.example.helloworld, PID: 1770
07-29 06:49:02.532: E/AndroidRuntime(1770): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.MainActivity}: android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar.
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.app.ActivityThread.access$800(ActivityThread.java:138)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.os.Looper.loop(Looper.java:136)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.app.ActivityThread.main(ActivityThread.java:5026)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at java.lang.reflect.Method.invokeNative(Native Method)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at java.lang.reflect.Method.invoke(Method.java:515)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at dalvik.system.NativeStart.main(Native Method)
07-29 06:49:02.532: E/AndroidRuntime(1770): Caused by: android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar.
07-29 06:49:02.532: E/AndroidRuntime(1770):     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:275)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2872)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3129)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:303)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.app.Activity.setContentView(Activity.java:1930)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at com.example.helloworld.MainActivity.onCreate(MainActivity.java:16)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.app.Activity.performCreate(Activity.java:5242)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-29 06:49:02.532: E/AndroidRuntime(1770):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
07-29 06:49:02.532: E/AndroidRuntime(1770):     ... 11 more
xml

清单

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2012 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="android.support.v7.appcompat">
    <uses-sdk android:minSdkVersion="7"/>
    <application />
</manifest>

将应用程序的版本设置为19,而不是20,并在19模拟器上运行,而不是20

在Manifest文件中,更改

android:targetSdkVersion="20"

android:targetSdkVersion="19"

并在API level 19模拟器上运行您的项目。

不要使用API Level 20和平台4.4W作为Android虚拟设备。当第19层和平台4.2.2设置在AVD上时,一切都正常运行。

查看这里的targetSdkVersion设置

你不能只写button_touch,在onCreate类型中this:

Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
})

ActivityManager:警告:Activity未启动,其当前任务已被带到前台

应用程序已部署。不需要重新安装

它通常发生在你的应用程序在前台(当前运行),你按下运行命令没有任何代码更改。

基于这个链接,你应该设置你的targetSdkVersion为19而不是20。

使用这个。对代码做了一点改动。扩展Activity而不是ActionBarActivity。'Activity'是所有类的父类。无需导入:"android.support.v7.app.ActionBarActivity;".

你的MainActivity.java文件:

package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
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 onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
public void button_touch(View v)
{
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText("Hello, world.");
}
}

和你的activity_main.xml是完美的

最新更新