你好我有问题从另一个类加载到android java的主类中的函数
package com.sourcebring.wordbring;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load default xml
setContentView(R.layout.main);
ntfi Notifi_engine = new ntfi();
Notifi_engine.Send_notifi("hi", "welcome", R.drawable.wb_logo);
}
}
这是我的班级
package com.inc.project;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Action;
import android.content.Intent;
import android.app.Activity;
import android.content.ContentResolver;
import android.support.v7.app.AppCompatActivity;
public class ntfi extends AppCompatActivity {
public void Send_notifi(String Title, String Content, int Icon)
{
// Create notification handler
NotificationCompat.Builder notifi_Builder = new NotificationCompat.Builder(this)
.setSmallIcon(Icon)
.setContentTitle(Title)
.setContentText(Content);
Intent resultIntent = new Intent(this, ntfi.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(WordbringMainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notifi_Builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notifi_Builder.build());
}
}
使用此代码我在程序上没有错误但是当我编译项目并且我想运行它时,我的android程序停止了。
但是当我不使用ntfi类并且我在
中使用此函数Send_notifi
时
MainActivity
没有问题,程序成功运行。我希望在单独的类中使用此函数Send_notifi
,如顶部代码...问题是什么?
这是我在运行Android应用程序时的错误
05-08 19:41:44.510 918-918/com.inc.project E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.inc.project/com.inc.project.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
at android.app.ActivityThread.access$600(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:3004)
at com.inc.project.ntfi.Send_notifi(ntfi.java:27)
at com.inc.project.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
at android.app.ActivityThread.access$600(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
这不起作用,因为NotificationCompat.Builder(...)
需要Context
个对象,因为您已经自己创建了ntfi
个实例(它不是由AppCompatActivity
创建的系统)即使它延伸Context
它也不是public class ntfi {
public void Send_notifi(Context context, String Title, String Content, int Icon) {
NotificationCompat.Builder notifi_Builder = new NotificationCompat.Builder(context)
.setSmallIcon(Icon)
.setContentTitle(Title)
.setContentText(Content);
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(WordbringMainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notifi_Builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notifi_Builder.build());
}
。
你可以做的是:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ntfi Notifi_engine = new ntfi();
Notifi_engine.Send_notifi(this, "hi", "welcome", R.drawable.wb_logo);
}
}
并显示通知:
align='center'
另外,作为旁注,您应该考虑遵循Java命名约定,使用大写字母启动类名,使用小写字母启动变量名。就像现在一样,你的代码很难阅读。
答案 1 :(得分:0)
您真的不需要ntfi
延长AppCompatActivity
。您所需要的只是在static
课程中定义一些ntfi
方法,然后在Activity
内调用这些方法。在这种情况下,您应该将Context传递给这些帮助器方法。以下是更改ntfi
课程的方法:
public class ntfi {
public static void Send_notifi(Context context, String Title, String Content, int Icon)
{
// Create notification handler
NotificationCompat.Builder notifi_Builder = new NotificationCompat.Builder(context)
.setSmallIcon(Icon)
.setContentTitle(Title)
.setContentText(Content);
Intent resultIntent = new Intent(context, ntfi.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(WordbringMainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notifi_Builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notifi_Builder.build());
}
}
然后您可以像这样调用Send_notifi
函数:
ntfi.Send_notifi("hi", "welcome", R.drawable.wb_logo);
请注意,在这种情况下,您不必实例化ntfi - 所以摆脱以下行:ntfi Notifi_engine = new ntfi();
。
顺便说一句,我希望这有助于为你的课程命名,就像ntfi(小写字母)一样,想出一些更具描述性的课程名称。