繁体   English   中英

Android应用程序不断崩溃,与ID有关

[英]Android app keeps crashing with something to do with id's

我目前正在尝试制作一个基本的android应用程序,并试图将我的头缠在一些崩溃上。

我尝试注释掉代码的各个部分,然后进行编译和运行。 据我所知,这似乎是我在第二个活动中同时拥有一个Button和TextView的时候。 我正在尝试使该应用将用户在主要活动中输入的文本转移到第二个活动中。 (按下按钮后)。 我正在尝试使第二个活动具有一个返回到第一个活动的按钮,并且还显示从主活动中输入的文本。

这是主要活动的代码:

package com.example.brochura;

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

public class Main extends Activity
{
    public final static String EXTRA_MESSAGE = "com.example.brochura.MESSAGE";

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

    public void sendMessage(View view)
    {

        Intent intent = new Intent(this, Deployed.class);
        EditText editText = (EditText)findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

这是第二个活动的代码:

package com.example.brochura;

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

public class Deployed extends Activity
{
    public final static String EXTRA_MESSAGE = "com.example.brochura.MESSAGE";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Button button = (Button) findViewById(R.id.return_button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                finish();
            }
        });

        Intent intent = getIntent();
        String message = intent.getStringExtra(Main.EXTRA_MESSAGE);

        setContentView(R.layout.deployed);
        TextView textView = (TextView) findViewById(R.id.message_text);
        textView.setText(message);

    }
}

当我尝试去第二个活动时发生崩溃(已部署)

这是Main和Deployed类的xml:

(main.xml中)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="horizontal"
    >
<EditText
    android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message"
/>
<Button
    android:id="@+id/deploy_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"
/>
</LinearLayout>

(deployed.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    >
<Button
    android:id="@+id/return_button"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="100dp"
    android:text="Return"
/>
<TextView
    android:id="@+id/message_text"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="100dp"
/>
</LinearLayout>

在此先感谢您,我对移动开发还很陌生,因此任何人都可以给我的建议也将不胜感激。

---更新:这是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.brochura"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19"
    />
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name=".Main"
                  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=".Deployed" />
    </application>
</manifest>

这是logcat

I/ActivityManager( 1180): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.brochura/.Main} from pid 1552
I/ActivityManager( 1180): Start proc com.example.brochura for activity com.example.brochura/.Main: pid=2013 uid=10052 gids={50052}
I/ActivityManager( 1180): Displayed com.example.brochura/.Main: +321ms
E/AndroidRuntime( 2013): Process: com.example.brochura, PID: 2013
E/AndroidRuntime( 2013):    at com.example.brochura.Main.sendMessage(Main.java:36)
W/ActivityManager( 1180):   Force finishing activity com.example.brochura/.Main
W/ActivityManager( 1180): Activity pause timeout for ActivityRecord{b131c5f0 u0 com.example.brochura/.Main t7 f}
W/InputDispatcher( 1180): channel 'b149a920 com.example.brochura/com.example.brochura.Main (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
E/InputDispatcher( 1180): channel 'b149a920 com.example.brochura/com.example.brochura.Main (server)' ~ Channel is unrecoverably broken and will be disposed!
I/ActivityManager( 1180): Process com.example.brochura (pid 2013) has died.
W/InputDispatcher( 1180): Attempted to unregister already unregistered input channel 'b149a920 com.example.brochura/com.example.brochura.Main (server)'
I/WindowState( 1180): WIN DEATH: Window{b149a920 u0 com.example.brochura/com.example.brochura.Main}
put this line in first actvity.

Intent i=new Intent(First.this,second.class);
                i.putExtra("strin", "HI this is my text");
                startActivity(i);







and put this line in second activity 

Intent i=getIntent();
    String str=i.getStringExtra("strin");


declare both activity in manifest 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM