![](/img/trans.png)
[英]errors when using the bluetooth sample code on android developer site
[英]sample code on android developer site crash
因此,我剛剛開始使用android進行開發,並逐步在開發站點上實現了示例代碼。
前幾個工作正常,但我堅持使用意圖。
我已經在開發人員站點上仔細閱讀了代碼及其確切副本。 那么,為什么活動在startActivity(intent)更改時會崩潰?
救命 !
這是第一個活動:::
package com.example.Trial;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisaplyMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
這是它稱為的活動:
package com.example.Trial;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisaplyMessageActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(TrialActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
} }
這是main.xml ::
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:hint="@string/edit_message" />
<Button
android:onClick="sendMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
這是string.xml :::
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
</resources>
FATAL EXCEPTION尾隨為:
06-18 21:30:51.377:E / AndroidRuntime(341):在com.example.Trial.TrialActivity.sendMessage(TrialActivity.java:23)
非常感謝
您的清單中是否包含DisplayMessageActivity
? 您的主要活動應有一個,其他活動也應有一個:
<activity
android:name=".TrialActivity"
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=".DisplayMessageActivity" >
</activity>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.