簡體   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