Jason Albertica Jason Albertica - 6 months ago 24
Java Question

Use function from other classes inside of main class android java

hello i have problem to load function from another class into main class in 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);

}


}


and this is my class

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());
}
}


with this code i have no error on program but when i compile project and i want to run it, my android program stopped.

but when i dont use ntfi class and i use this function
Send_notifi
in

MainActivity


there is no problem and program work successfully. i want to have this function
Send_notifi
in Separate class like top codes... what is the problem?

this is my error while run android app

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) 

Answer

This doesn't work because NotificationCompat.Builder(...) requires a Context object and since you've created the ntfi instance by yourself (it wasn't created by the system) even though it extends AppCompatActivity it isn't a valid Context.

What you can do instead is this:

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());
  }

and to show a notification:

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);
    }
}

Also, as a side note, you should consider following the Java naming conventions, start class names with a uppercase letter and variable names with a lowercase letter. As it is now, your code is very hard to read.