我只是想为推送通知创建示例应用但收到错误,这是我的活动。应用程序崩溃在下面显示的行注释。
public class MainActivity extends ActionBarActivity {
EditText ed1,ed2,ed3;
public NotificationManager notificationManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
Button b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tittle = ed1.getText().toString().trim();
String subject = ed2.getText().toString().trim();
String body = ed3.getText().toString().trim();
Notification.Builder builder = new Notification.Builder(MainActivity.this);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
builder.setSmallIcon(R.drawable. menu1)
.setContentIntent(pendingIntent)
.setContentTitle(tittle)
.setContentText(subject)
.setSubText(body);
Notification notification = builder.getNotification();
//App crashing while executing this line
notificationManager.notify(0, notification);
}
});
}}
这是我的布局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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TESTING"
android:id="@+id/textView2"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginTop="22dp"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:hint="Name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:hint="Subject"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText3"
android:hint="Body"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_alignEnd="@+id/editText2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:id="@+id/button"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true" /></RelativeLayout>
现在应用程序没有崩溃,但是点击按钮
时没有显示通知LOGCAT错误
12-24 14:34:28.491 20905-20905/com.example.siva.push D/OpenGLRenderer﹕ Enabling debug mode 0
12-24 14:34:28.571 20905-20905/com.example.siva.push I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41d33cd8 time:125753735
12-24 14:34:32.701 20905-20905/com.example.siva.push W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection
12-24 14:34:32.701 20905-20905/com.example.siva.push W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection
12-24 14:34:33.881 20905-20905/com.example.siva.push W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection
12-24 14:34:33.881 20905-20905/com.example.siva.push W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection
12-24 14:34:35.151 20905-20905/com.example.siva.push V/AudioManager﹕ playSoundEffect effectType: 0
12-24 14:34:35.151 20905-20905/com.example.siva.push V/AudioManager﹕ querySoundEffectsEnabled...
12-24 14:34:42.641 20905-20905/com.example.siva.push V/AudioManager﹕ playSoundEffect effectType: 0
12-24 14:34:42.641 20905-20905/com.example.siva.push V/AudioManager﹕ querySoundEffectsEnabled...
12-24 14:34:47.261 20905-20905/com.example.siva.push V/AudioManager﹕ playSoundEffect effectType: 0
12-24 14:34:47.261 20905-20905/com.example.siva.push V/AudioManager﹕ querySoundEffectsEnabled...
12-24 14:34:52.411 20905-20905/com.example.siva.push V/AudioManager﹕ playSoundEffect effectType: 0
12-24 14:34:52.411 20905-20905/com.example.siva.push V/AudioManager﹕ querySoundEffectsEnabled...
答案 0 :(得分:2)
初始化NotificationManager变量
NotificationManager notificationManager= (NotificationManager)MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
注意: getNotification()
已弃用。更好地使用builder.build()
代替