Android Hello World Tutorial在测试时停止

时间:2015-02-05 05:56:48

标签: java android xml

我参与Android教程的“启动另一个活动”部分,当我安装并测试它时,它根本不起作用。

它编译得很好,但是在点击发送按钮后运行它会中断。

我在Ubuntu 12.04上使用命令行工具并安装到真实设备Galaxy S5。

我知道logcat但是无法让它工作,它要么根本没有显示输出,要么提供大量垃圾邮件输出我无法跟上。如果我能够设法隔离我的应用程序的输出并切断其他所有内容,我会很乐意提供logcat信息。

我在这方面看到了很多类似的问题,这让我相信教程编写得不是很好。

这是“MyActivity.java”

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.EditText;
import android.view.View;

public class MyActivity extends Activity
{
    public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";

    /** Called when the activity is first created */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    /** Called when the user clicks the send button */
    public void sendMessage(View view)
    {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);

        startActivity(intent);
    }
}

这是DisplayMessageActivity

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.*;
import android.view.*;
import android.widget.*;
import com.example.myfirstapp.R;

public class DisplayMessageActivity extends ActionBarActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //Get the message from intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

        //Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        //Set the text view as the activity layout
        setContentView(textView);
    }
}

这是AndroidManifest.xml

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

    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">

        <activity
            android:name="MyActivity"
            android:label="@string/app_name">

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

        <activity
            android:name="com.example.myfirstapp.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.myfirstapp.MyActivity">

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MyActivity"
            />
        </activity>

    </application>
</manifest>

编辑:这是Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>

<EditText android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message"
/>

<Button
    android:layout_weight="1"
        android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"
/>

</LinearLayout>

有一些代码我改变并取出,因为它说要添加代码,但后来会显示“这就是你的代码应该是什么样子”,并且它缺少一些原始代码。我还更改了活动中的清单包名称,因为它在开始时使用com.example.myfirstapp但稍后使用com.mycompany.myfirstapp

非常感谢任何关于为什么这个看似简单的教程不起作用的帮助或建议。

编辑:

Logcat输出 - (按下发送按钮后)

I/Timeline( 3812): Timeline: Activity_launch_request id:com.example.myfirstapp time:74636924
D/AndroidRuntime( 3812): Shutting down VM
E/AndroidRuntime( 3812): FATAL EXCEPTION: main
E/AndroidRuntime( 3812): Process: com.example.myfirstapp, PID: 3812
E/AndroidRuntime( 3812): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime( 3812):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
E/AndroidRuntime( 3812):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
E/AndroidRuntime( 3812):    at android.app.ActivityThread.access$900(ActivityThread.java:172)
E/AndroidRuntime( 3812):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
E/AndroidRuntime( 3812):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 3812):    at android.os.Looper.loop(Looper.java:145)
E/AndroidRuntime( 3812):    at android.app.ActivityThread.main(ActivityThread.java:5834)
E/AndroidRuntime( 3812):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 3812):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 3812):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
E/AndroidRuntime( 3812):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
E/AndroidRuntime( 3812): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime( 3812):    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
E/AndroidRuntime( 3812):    at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
E/AndroidRuntime( 3812):    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
E/AndroidRuntime( 3812):    at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:16)
E/AndroidRuntime( 3812):    at android.app.Activity.performCreate(Activity.java:6221)
E/AndroidRuntime( 3812):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
E/AndroidRuntime( 3812):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
E/AndroidRuntime( 3812):    ... 10 more
I/Process ( 3812): Sending signal. PID: 3812 SIG: 9
I/ActivityManager(  871): Process com.example.myfirstapp (pid 3812)(adj 13) has died(104,205)

3 个答案:

答案 0 :(得分:1)

这是工作代码:

MainActivity.class

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.editText1);
    }

    public void Go(View v)
    {
        Log.d("check", "Pressed");

        String value = et.getText().toString();
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("key", value);
        startActivity(intent);
    }   
}

SecondActivity.class

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.TextView;

public class SecondActivity extends ActionBarActivity {

    TextView textv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_activity);

        String txt = getIntent().getStringExtra("key");
        Log.d("check", "got : " +txt);

        textv = (TextView) findViewById(R.id.textView1);
        textv.setText(txt);

    }

}

RES /布局/ 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"
    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.testproject.MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="67dp"
        android:layout_marginTop="46dp"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="Go"
        android:text="Button" />

</RelativeLayout>

RES /布局/ new_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <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="74dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

AndroidManifest.xml

<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>
                <action android:name="android.intent.action.MAIN" />

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

        <activity android:name=".SecondActivity"></activity> // Registered the newly created activity

    </application>

输出:

enter image description here

enter image description here

Logcat:

enter image description here

通过查看您的LOGCAT ,我的主题出现了问题。

你没有在AndroidManifest.xml file

中宣布AppTheme
android:theme="@style/AppTheme"

答案 1 :(得分:0)

试试我为你做的这个示例代码。

mainactivity.xml代码

<EditText 
android:id="@+id/edtMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="message"
/>
<Button 
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
/>

MainActivity.class

按钮btnSend;   EditText edtMessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edtMessage=(EditText)findViewById(R.id.edtMessage);
    btnSend=(Button)findViewById(R.id.btnSend);

    btnSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
        String Message=edtMessage.getText().toString();
        Intent intent=new Intent(getApplicationContext(),Second.class);
        intent.putExtra("Message", Message);
        startActivity(intent);

        }
    });

} 

second.xml

<TextView 
android:id="@+id/tviMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>

Second.class

TextView tviMessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    tviMessage=(TextView)findViewById(R.id.tviMessage);

    Bundle extras = getIntent().getExtras();
     String message = extras.getString("Message");
    tviMessage.setText(message);
    }

Manifesto.xml

   <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>
            <action android:name="android.intent.action.MAIN" />

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

     <activity
        android:name=".Second"
        android:label="@string/app_name" >
   </activity>

答案 2 :(得分:-1)

使用带有此命令的logcat:

adb logcat | grep `adb shell ps | grep com.example.myfirstapp | cut -c10-15`

我能够找到导致应用崩溃的错误。它来自错过这条线:

android:theme="@style/Theme.AppCompat.Light"

在Manifest.xml文件中。这是活动栏兼容性类所必需的。这部分在教程中被完全忽略了,大概是因为IDE会自动完成这部分。

感谢大家的帮助和建议,这帮助我不断寻找和尝试,直到我找到了解决方案。