补充工具栏菜单添加其他活动,但应用崩溃并出现错误
如何在每项活动中添加补充工具栏菜单
** Siderbar Code **
package com.example.bulksms;
import java.util.ArrayList;
import com.example.bulksms.adapter.NavDrawerListAdapter;
import com.example.bulksms.model.NavDrawerItem;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.bulksms.R;
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public class Home extends ActionBarActivity {
protected DrawerLayout mDrawerLayout;
protected ListView mDrawerList;
protected ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
protected String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
//if TheradPolicy When app crash automatically.
//StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
@SuppressLint("NewApi")
protected void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new LogoutFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
enter code here
package com.example.bulksms;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.StrictMode;
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public class Sendsms extends Home {
String id;
InputStream is=null;
String result=null;
String line=null;
public Typeface font;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sendsms);
// FontAwesome Icon
font = Typeface.createFromAsset( getAssets(), "fonts/fontawesome-webfont.ttf" );
//
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
Button select=(Button) findViewById(R.id.send_bulk_sms);
Button logout=(Button) findViewById(R.id.logout);
logout.setTypeface(font);
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
select();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
/**
* Function: Select Phone Number To Send Sms.
* using Http Request.
* Get the Response and calling The SmsSend functions.
*/
public void select() throws JSONException, ClientProtocolException, IOException
{
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ayattechnologies.com/demo/sentsms/getNumber.php?Getdata=true");
//httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
//
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
JSONObject obj_resultParent = new JSONObject();
JSONArray obj_resultarray = new JSONArray();
//
try
{
JSONArray aJson = new JSONArray(result); //Json Get From Server
// create apps list
for(int i=0; i<aJson.length(); i++)
{
JSONObject json = aJson.getJSONObject(i);
//
String senderNumber = json.getString("phoneNumber");
String subject = json.getString("subject");
String message = json.getString("message");
String Id = json.getString("id");
Toast.makeText(getBaseContext(), "SenderNumber : "+senderNumber+"Messages: "+message,
Toast.LENGTH_SHORT).show();
//
boolean smsresult = sendSMS(senderNumber,subject, message);
if(smsresult)
{
JSONObject json_resultget = new JSONObject();
json_resultget.put("Id", Id); // Set the first name/pair
json_resultget.put("status",smsresult);
obj_resultarray.put(json_resultget);
} //
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
obj_resultParent.put("smsresults", obj_resultarray);
Toast.makeText(getBaseContext(), "SenderNumber : "+obj_resultParent.toString(),Toast.LENGTH_SHORT).show();
//
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ayattechnologies.com/demo/sentsms/getNumber.php?Postdata=true");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("smsresults",obj_resultParent.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Toast.makeText(getBaseContext(), "SenderNumber : "+response,Toast.LENGTH_SHORT).show();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
//
}
/**
* Function: SendSms function send sms
* @param: SenderNumberTo String use for sender number
* @param:Subject.
* @param:Message.
* */
public boolean sendSMS(String senderNumberto,String subject, String message) {
// TODO Auto-generated method stub
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(senderNumberto, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",Toast.LENGTH_LONG).show();
return true;
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Sending SMS failed.",Toast.LENGTH_LONG).show();
e.printStackTrace();
return false;
}
}
}
12-31 13:04:43.410:E / AndroidRuntime(13472):致命异常:主要 12-31 13:04:43.410:E / AndroidRuntime(13472):进程:com.example.bulksms,PID:13472 12-31 13:04:43.410:E / AndroidRuntime(13472):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.bulksms / com.example.bulksms.Sendsms}:java.lang.NullPointerException 12-31 13:04:43.410:E / AndroidRuntime(13472):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367) 12-31 13:04:43.410:E / AndroidRuntime(13472):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419) 12-31 13:04:43.410:E / AndroidRuntime(13472):在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 12-31 13:04:43.410:E / AndroidRuntime(13472):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1342) 12-31 13:04:43.410:E / AndroidRuntime(13472):在android.os.Handler.dispatchMessage(Handler.java:110) 12-31 13:04:43.410:E / AndroidRuntime(13472):在android.os.Looper.loop(Looper.java:193) 12-31 13:04:43.410:E / AndroidRuntime(13472):在android.app.ActivityThread.main(ActivityThread.java:5323) 12-31 13:04:43.410:E / AndroidRuntime(13472):at java.lang.reflect.Method.invokeNative(Native Method) 12-31 13:04:43.410:E / AndroidRuntime(13472):at java.lang.reflect.Method.invoke(Method.java:515) 12-31 13:04:43.410:E / AndroidRuntime(13472):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:828) 12-31 13:04:43.410:E / AndroidRuntime(13472):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 12-31 13:04:43.410:E / AndroidRuntime(13472):at dalvik.system.NativeStart.main(Native Method) 12-31 13:04:43.410:E / AndroidRuntime(13472):引起:java.lang.NullPointerException 12-31 13:04:43.410:E / AndroidRuntime(13472):at com.example.bulksms.Sendsms.onCreate(Sendsms.java:63) 12-31 13:04:43.410:E / AndroidRuntime(13472):在android.app.Activity.performCreate(Activity.java:5343) 12-31 13:04:43.410:E / AndroidRuntime(13472):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 12-31 13:04:43.410:E / AndroidRuntime(13472):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331) 12-31 13:04:43.410:E / AndroidRuntime(13472):... 11更多
**After remove Inflator only extends class **
**错误**
12-31 15:06:13.951: E/(29235): appName=com.example.bulksms, acAppName=/system/bin/surfaceflinger
12-31 15:06:13.951: E/(29235): 0
12-31 15:06:13.951: E/(29235): appName=com.example.bulksms, acAppName=/system/bin/surfaceflinger
12-31 15:06:13.951: E/(29235): 0
12-31 15:06:17.024: E/FragmentManager(29235): No view found for id 0x7f090056 (com.example.bulksms:id/frame_container) for fragment HomeFragment{4237a568 #0 id=0x7f090056}
12-31 15:06:17.024: E/FragmentManager(29235): Activity state:
12-31 15:06:17.027: E/FragmentManager(29235): Local FragmentActivity 4236f7a8 State:
12-31 15:06:17.027: E/FragmentManager(29235): mCreated=falsemResumed=false mStopped=false mReallyStopped=false
12-31 15:06:17.027: E/FragmentManager(29235): mLoadersStarted=false
12-31 15:06:17.027: E/FragmentManager(29235): FragmentManager misc state:
12-31 15:06:17.027: E/FragmentManager(29235): mHost=android.support.v4.app.FragmentActivity$HostCallbacks@4236fa48
12-31 15:06:17.027: E/FragmentManager(29235): mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@4236fa48
12-31 15:06:17.027: E/FragmentManager(29235): mCurState=1 mStateSaved=false mDestroyed=false
12-31 15:06:17.027: E/FragmentManager(29235): View Hierarchy:
12-31 15:06:17.027: E/FragmentManager(29235): com.android.internal.policy.impl.PhoneWindow$DecorView{42370770 V.E..... ... 0,0-0,0}
12-31 15:06:17.028: E/FragmentManager(29235): android.widget.LinearLayout{42370ea8 V.E..... ... 0,0-0,0}
12-31 15:06:17.028: E/FragmentManager(29235): android.view.ViewStub{42371950 G.E..... ... 0,0-0,0 #1020313}
12-31 15:06:17.028: E/FragmentManager(29235): android.widget.FrameLayout{42371bf8 V.E..... ... 0,0-0,0}
12-31 15:06:17.028: E/FragmentManager(29235): android.support.v7.internal.widget.ActionBarOverlayLayout{42373630 V.ED.... ... 0,0-0,0 #7f09003c app:id/decor_content_parent}
12-31 15:06:17.029: E/FragmentManager(29235): android.support.v7.internal.widget.ContentFrameLayout{42373d90 V.ED.... ... 0,0-0,0 #1020002 android:id/content}
12-31 15:06:17.029: E/FragmentManager(29235):
12-31 15:06:17.030: E/FragmentManager(29235): android.widget.ScrollView{4237a950 VFED.V.. ... 0,0-0,0 #7f09004d app:id/scrollView1}
12-31 15:06:17.030: E/FragmentManager(29235): android.widget.LinearLayout{4237b640 V.E..... ... 0,0-0,0}
12-31 15:06:17.030: E/FragmentManager(29235): android.widget.LinearLayout{4237ba08 V.E..... ... 0,0-0,0}
12-31 15:06:17.030: E/FragmentManager(29235): android.support.v7.widget.AppCompatTextView{4237be20 V.ED.... ... 0,0-0,0}
12-31 15:06:17.030: E/FragmentManager(29235): android.widget.LinearLayout{4237c438 V.E..... ... 0,0-0,0}
12-31 15:06:17.030: E/FragmentManager(29235): android.widget.LinearLayout{4237c798 V.E..... ... 0,0-0,0}
12-31 15:06:17.031: E/FragmentManager(29235): android.widget.ImageView{4237cf30 V.ED.... ... 0,0-0,0}
12-31 15:06:17.031: E/FragmentManager(29235): android.support.v7.widget.AppCompatTextView{4237d2a0 VFED.... ... 0,0-0,0 #7f09004f app:id/tvInvisibleError}
12-31 15:06:17.031: E/FragmentManager(29235): android.widget.LinearLayout{4237d818 V.E..... ... 0,0-0,0}
12-31 15:06:17.031: E/FragmentManager(29235): android.support.v7.widget.AppCompatButton{4237db78 VFED..C. ... 0,0-0,0 #7f090065 app:id/send_bulk_sms}
12-31 15:06:17.031: E/FragmentManager(29235): android.support.v7.widget.AppCompatButton{4237e328 VFED..C. ... 0,0-0,0 #7f090066 app:id/logout}
12-31 15:06:17.032: E/FragmentManager(29235): android.support.v7.internal.widget.ActionBarContainer{42374258 V.ED.... ... 0,0-0,0 #7f09003d app:id/action_bar_container}
12-31 15:06:17.032: E/FragmentManager(29235): android.support.v7.widget.Toolbar{42374820 V.E..... ... 0,0-0,0 #7f09003e app:id/action_bar}
12-31 15:06:17.033: E/FragmentManager(29235): android.widget.TextView{423773f0 V.ED.... ... 0,0-0,0}
12-31 15:06:17.033: E/FragmentManager(29235): android.widget.ImageButton{42374fd0 VFED..C. ... 0,0-0,0}
12-31 15:06:17.033: E/FragmentManager(29235): android.support.v7.internal.widget.ActionBarContextView{42376050 G.E..... ... 0,0-0,0 #7f09003f app:id/action_context_bar}
12-31 15:06:17.040: E/AndroidRuntime(29235): FATAL EXCEPTION: main
12-31 15:06:17.040: E/AndroidRuntime(29235): Process: com.example.bulksms, PID: 29235
12-31 15:06:17.040: E/AndroidRuntime(29235): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bulksms/com.example.bulksms.Sendsms}: java.lang.IllegalArgumentException: No view found for id 0x7f090056 (com.example.bulksms:id/frame_container) for fragment HomeFragment{4237a568 #0 id=0x7f090056}
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.ActivityThread.access$800(ActivityThread.java:151)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.os.Handler.dispatchMessage(Handler.java:110)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.os.Looper.loop(Looper.java:193)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.ActivityThread.main(ActivityThread.java:5323)
12-31 15:06:17.040: E/AndroidRuntime(29235): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 15:06:17.040: E/AndroidRuntime(29235): at java.lang.reflect.Method.invoke(Method.java:515)
12-31 15:06:17.040: E/AndroidRuntime(29235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
12-31 15:06:17.040: E/AndroidRuntime(29235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
12-31 15:06:17.040: E/AndroidRuntime(29235): at dalvik.system.NativeStart.main(Native Method)
12-31 15:06:17.040: E/AndroidRuntime(29235): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f090056 (com.example.bulksms:id/frame_container) for fragment HomeFragment{4237a568 #0 id=0x7f090056}
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.BackStackRecord.run(BackStackRecord.java:698)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.Activity.performStart(Activity.java:5352)
12-31 15:06:17.040: E/AndroidRuntime(29235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
12-31 15:06:17.040: E/AndroidRuntime(29235): ... 11 more