我正在关注此示例http://examples.javacodegeeks.com/android/core/ui/checkbox-ui/android-checkbox-example/以向我的添加添加复选框,但每次添加addListenerOnChkWindows();
和addListenerOnButton();
时,应用程序在启动时崩溃。继承我的代码:
public class MainActivity extends Activity {
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
private Button shareButton;
private CheckBox linux, macos, windows;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnChkWindows();
addListenerOnButton();
mTitle = mDrawerTitle = getTitle();
sendNotification(mTitle.toString());
// 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
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
// 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
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().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) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().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
* */
private 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 Posted();
break;
case 2:
fragment = new Settings();
break;
case 3:
fragment = new About();
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");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().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);
}
/**
* Send a notification message
*/
//public void sendNotification(String title) {
//String content = "Tap to share";
//NotificationCompat.Builder builder = new NotificationCompat.Builder(
// this).setContentTitle(title).setContentText(content)
// .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(false).setOngoing(true);
/// NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Intent resultIntent = new Intent(this, MainActivity.class);
// TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
//stackBuilder.addNextIntent(resultIntent);
// PendingIntent resultPendingIntent =
/// stackBuilder.getPendingIntent(
//// 0,
// PendingIntent.FLAG_UPDATE_CURRENT
// );
//Builder mBuilder = null;
//mBuilder.setContentIntent(resultPendingIntent);
//nMgr.notify(1, builder.build());
//
//}
public void sendNotification(String title) {
String content = "Tap to share";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setContentTitle(title).setContentText(content)
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(false).setOngoing(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(1, mBuilder.build());
}
public void addListenerOnChkWindows() {
windows = (CheckBox) findViewById(R.id.shareButton);
windows.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(MainActivity.this,"Bro, try Linux :)", Toast.LENGTH_LONG).show();
}
}
});
}
public void addListenerOnButton() {
linux = (CheckBox) findViewById(R.id.fb);
macos = (CheckBox) findViewById(R.id.tt);
windows = (CheckBox) findViewById(R.id.tr);
button = (Button) findViewById(R.id.shareButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("Linux check : ").append(linux.isChecked());
result.append("\nMac OS check : ").append(macos.isChecked());
result.append("\nWindows check :").append(windows.isChecked());
Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
复选框的代码位于最底部
这是堆栈跟踪:https://drive.google.com/file/d/0B98htuh05n20eXBiN3p6MTBHVHM/view?usp=sharing
继承我的XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.openshareproo.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/shareButton"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:text="@string/share" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignLeft="@+id/section_label"
android:layout_alignTop="@+id/fb"
android:src="@drawable/ic_fb" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/textView2"
android:src="@drawable/ic_tt" />
<CheckBox
android:id="@+id/tt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView2"
android:layout_alignRight="@+id/View01"
android:text="Tap to Share"
android:checked="true"
/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignRight="@+id/view1"
android:text="Tap to Share" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/section_label"
android:layout_alignTop="@+id/section_label"
android:layout_toLeftOf="@+id/button3"
android:ems="10" >
<requestFocus />
</EditText>
<CheckBox
android:id="@+id/tr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView3"
android:layout_alignRight="@+id/View01"
android:text="Tap to Share"
/>
<View
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignLeft="@+id/imageView2"
android:layout_below="@+id/imageView1"
android:layout_marginTop="14dp"
android:background="@android:color/darker_gray" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignBottom="@+id/button1"
android:layout_toLeftOf="@+id/button1"
android:text="Web"
android:onClick="buttonMe" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fb"
android:layout_below="@+id/view1"
android:layout_marginTop="10dp"
android:text="Twitter"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35dp" />
<TextView
android:id="@+id/fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_marginTop="34dp"
android:layout_toLeftOf="@+id/checkBox1"
android:text="Facebook"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35dp" />
<View
android:id="@+id/View01"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignLeft="@+id/imageView3"
android:layout_below="@+id/imageView2"
android:layout_marginTop="14dp"
android:background="@android:color/darker_gray" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/View01"
android:layout_marginTop="11dp"
android:layout_toRightOf="@+id/imageView2"
android:text="Tumblr"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignTop="@+id/textView3"
android:layout_toLeftOf="@+id/textView3"
android:src="@drawable/ic_tr" />
</RelativeLayout>
答案 0 :(得分:1)
堆栈跟踪对回答您的问题很有用。
但是,我愿意打赌问题在于您将shareButton
定义为Button
的xml布局,而在addListenerOnChkWindows()的代码中,您将其转换为CheckBox
。您可能希望在该方法中使用R.id.tr
而不是R.id.shareButton
?
更新:
在addListenerOnChkWindows()
更改行:
windows =(CheckBox)findViewById(R.id.shareButton);
为:
windows =(CheckBox)findViewById(R.id.tr);