我的第一个应用程序displaymessageactivity不会显示 - Looper.java - 没有错误

时间:2013-12-03 00:39:50

标签: java android eclipse android-intent import

我现在调试并尝试了许多不同的东西,但是在我点击发送按钮后,没有任何反应,没有错误,但在调试模式下,我将程序跟踪到Looper.java,它似乎陷入困境。程序永远不会到达DisplayMessageActivity。

//切断来自Looper.java

/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
//PROGRAM JUMPS HERE AND...
public static void loop() {
    final Looper me = myLooper();
    if (me == null) {
    throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;

// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();

for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return; 
//here my program returns to the top, here: public static void loop() { and comes to   this return-command again, over and over.
}

这是我的文件:DisplayMessageActivity.java

public class DisplayMessageActivity extends Activity {

@SuppressLint("NewApi")

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);

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

// Show the Up button in the action bar.
//setupActionBar();
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
/*  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}*/

//Få tak i intenten som kalte på dette objektet
Intent intent = getIntent();
if(intent != null) {
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
if(message != null) {
textView.setText(message);
}
}   

MainActivity.java

public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
System.out.println("sendMessage");
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);
//OK TO THIS PLACE...
}
}

// Set the text view as the activity layout
setContentView(textView);
// Show the Up button in the action bar.
}

/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
/*@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
} */
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
*/

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:     
// This ID represents the Home or Up button. In the case of this
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}

的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" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myfirstapp.MainActivity"
android:parentActivityName="com.example.myfirstapp.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="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>

2 个答案:

答案 0 :(得分:0)

您没有使用startActivity(Intent intent)意图致电sendMessage(),因此不会做任何事情。

答案 1 :(得分:0)

你必须启动你的意图, 试试这段代码

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);}