我的应用程序以意想不到的方式崩溃。每当我运行我的应用程序的主要活动时,它就会被执行。但是当我点击ID为“R.id.msgnow”的按钮时,我的应用程序崩溃了。我尝试了之前在stackoverflow中给出的所有解决方案,验证了我的manifest.xml,一切似乎都正确。但我仍然无法弄清楚崩溃的原因是什么。任何建议都会非常有用。谢谢
MainActivity.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button now = (Button) findViewById(R.id.msgnow);
assert now != null;
now.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
try {
Intent i = new Intent(MainActivity.this, Msg_now.class);
startActivity(i);
}
catch (ActivityNotFoundException e){
e.printStackTrace();
}
// Toast.makeText(getApplicationContext(), "Add SMS..", Toast.LENGTH_LONG).show();
}
});
}
}
Msg_now.class
public class Msg_now extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_msg_now);
EditText cN = (EditText) findViewById(R.id.enter_recipients);
EditText msgContent = (EditText) findViewById(R.id.msg_content);
Button find = (Button) findViewById(R.id.add_contacts);
assert find != null;
final String contactName = cN.getText().toString();
final String messageContent = msgContent.getText().toString();
Button send = (Button) findViewById(R.id.msg_send);
Intent myIntent = new Intent(this,Contacts.class);
myIntent.putExtra("mytext",contactName);
startActivity(myIntent);
assert send != null;
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(contactName, null, messageContent, null, null);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
Toast.makeText(Msg_now.this, "Recepient or message content missing.", Toast.LENGTH_SHORT).show();
}
}
});
find.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent i = new Intent(Msg_now.this, Contacts.class);
startActivity(i);
}
});
}
}
Contacts.java
public class Contacts extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ContactsFragment fragment = new ContactsFragment();
fragmentTransaction.add(R.id.contacts, fragment);
fragmentTransaction.commit();
/*getFragmentManager().beginTransaction()
.add(R.id.contacts, new ContactsFragment() )
.commit();*/
}
}
ContactFragment.java
public class ContactsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener {
// The column index for the _ID column
private static final int CONTACT_ID_INDEX = 0;
// The column index for the LOOKUP_KEY column
private static final int LOOKUP_KEY_INDEX = 1;
@SuppressLint("InlinedApi")
private static final String[] PROJECTION =
{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
ContactsContract.Contacts.DISPLAY_NAME
};
@SuppressLint("InlinedApi")
private static final String SELECTION =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " LIKE ?" :
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?";
// Defines a variable for the search string
Intent intent = getActivity().getIntent();
private String mSearchString = intent.getStringExtra("mytext");
// Defines the array to hold values that replace the ?
private String[] mSelectionArgs = { mSearchString };
private final static String[] FROM_COLUMNS = {
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
ContactsContract.Contacts.DISPLAY_NAME
};
private final static int[] TO_IDS = {
android.R.id.text1
};
ListView mContactsList;
// Define variables for the contact the user selects
// The contact's _ID value
long mContactId;
// The contact's LOOKUP_KEY
String mContactKey;
// A content URI for the selected contact
Uri mContactUri;
// An adapter that binds the result Cursor to the ListView
private SimpleCursorAdapter mCursorAdapter;
//constructor
public ContactsFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the fragment layout
return inflater.inflate(R.layout.contacts,container, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Gets the ListView from the View list of the parent activity
mContactsList = (ListView) getActivity().findViewById(R.id.list);
// Gets a CursorAdapter
mCursorAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.contacts,
null,
FROM_COLUMNS, TO_IDS,
0);
mContactsList.setAdapter(mCursorAdapter);
mContactsList.setOnItemClickListener(this);
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onItemClick(
AdapterView<?> parent, View item, int position, long rowID) {
// Get the Cursor
Cursor cursor = mCursorAdapter.getCursor();
// Move to the selected contact
cursor.moveToPosition(position);
// Get the _ID value
mContactId = Long.valueOf(CONTACT_ID_INDEX);
// Get the selected LOOKUP KEY
mContactKey = Integer.toString(LOOKUP_KEY_INDEX);
// Create the contact's content Uri
mContactUri = ContactsContract.Contacts.getLookupUri(mContactId, mContactKey);
/*
* You can use mContactUri as the content URI for retrieving
* the details for a contact.
*/
}
@Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
/*
* Makes search string into pattern and
* stores it in the selection array
*/
mSelectionArgs[0] = "%" + mSearchString + "%";
// Starts the query
return new CursorLoader(
getActivity(),
ContactsContract.Contacts.CONTENT_URI,
PROJECTION,
SELECTION,
mSelectionArgs,
null
);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// Put the result Cursor in the adapter for the ListView
mCursorAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// Delete the reference to the existing Cursor
mCursorAdapter.swapCursor(null);
}
}
---
我的错误 ------崩溃开始
11-05 00:39:21.040 2393-2393/com.example.nishant.textontym E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nishant.textontym, PID: 2393
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nishant.textontym/com.example.nishant.textontym.Msg_now}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.nishant.textontym/com.example.nishant.textontym.ContactsFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.nishant.textontym/com.example.nishant.textontym.ContactsFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at com.example.nishant.textontym.Msg_now.onCreate(Msg_now.java:27)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-05 00:39:26.263 2393-2393/com.example.nishant.textontym I/Process: Sending signal. PID: 2393 SIG: 9
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nishant.textontym">
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Contacts" />
<activity android:name=".Msg_now"></activity>
</application>
答案 0 :(得分:0)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.nishant.textontym/com.example.nishant.textontym.ContactsFragment}; have you declared this activity in your AndroidManifest.xml?
您的通话com.example.nishant.textontym.ContactsFragment
活动未在清单中声明或者如果ContactsFragment是片段,那么您必须将其添加到活动中,您无法通过意图进行调用。