无法从真实设备运行Android应用程序

时间:2014-04-15 07:27:25

标签: android android-intent adt

我是Android开发的新手。试图将我的第一个程序从ADT运行到真实设备。但我收到了以下错误..

ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.sendsmstest/.MainActivity }  

在我的Nexus 4中,我的小弹出窗口“不幸的是myapp已经停止了”。

以下是我的AndroidMenifest.xml

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

 <uses-permission android:name="android.permission.SEND_SMS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.sendsmstest.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>
</application>      

 </manifest>

下面是MainActivity.java代码。

package com.example.sendsmstest;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

 public class MainActivity extends Activity {

private EditText phoneNumber;
private EditText smsBody;
private Button smsManagerBtn;
private Button smsSendToBtn;
private Button smsViewBtn;

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

    phoneNumber = (EditText) findViewById(R.id.editNumber);
    smsBody = (EditText) findViewById(R.id.editSMSBody);
    smsManagerBtn = (Button) findViewById(R.id.buttonSend);


    smsManagerBtn.setOnClickListener(new OnClickListener() {
         public void onClick(View view) {
             sendSmsByManager();
         }
    });

    smsSendToBtn.setOnClickListener(new OnClickListener() {
         public void onClick(View view) {
             sendSmsBySIntent();
         }
    });

    smsViewBtn.setOnClickListener(new OnClickListener() {
         public void onClick(View view) {
             sendSmsByVIntent();
         }
    });

}

public void sendSmsByManager() {
    try {
        // Get the default instance of the SmsManager
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber.getText().toString(), 
                null,  
                smsBody.getText().toString(), 
                null, 
                null);
        Toast.makeText(getApplicationContext(), "Your sms has successfully sent!",
                Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(),"Your sms has failed...",
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }
}

public void sendSmsBySIntent() {
    // add the phone number in the data
    Uri uri = Uri.parse("smsto:" + phoneNumber.getText().toString());

    Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
    // add the message at the sms_body extra field
    smsSIntent.putExtra("sms_body", smsBody.getText().toString());
    try{
        startActivity(smsSIntent);
    } catch (Exception ex) {
        Toast.makeText(MainActivity.this, "Your sms has failed...",
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }
}

public void sendSmsByVIntent() {

    Intent smsVIntent = new Intent(Intent.ACTION_VIEW);
    // prompts only sms-mms clients
    smsVIntent.setType("vnd.android-dir/mms-sms");

    // extra fields for number and message respectively
    smsVIntent.putExtra("address", phoneNumber.getText().toString());
    smsVIntent.putExtra("sms_body", smsBody.getText().toString());
    try{
        startActivity(smsVIntent);
    } catch (Exception ex) {
        Toast.makeText(MainActivity.this, "Your sms has failed...",
                Toast.LENGTH_LONG).show();
        ex.printStackTrace();
    }

}
}

同样在我的logcat中,它显示了这个..

04-15 04:04:41.670: I/Process(1153): Sending signal. PID: 1153 SIG: 9

0 个答案:

没有答案